[FIXED] cannot pass the values in the initializes flutter(How can I receive those values?)

Issue

I want to pass the values from user screen where I have the values but on receving it in the usernavigationscreen I get them null. How can I receive those values?

class UserNavigationScreen extends StatefulWidget {
  int uid;
  String name, username, email;
  UserNavigationScreen(
      {Key? key,
      required this.uid,
      required this.name,
      required this.username,
      required this.email})
      : super(key: key);
  static const String id = 'user_navigation_screen';

  @override
  State<UserNavigationScreen> createState() =>
      _UserNavigationScreenState(uid, name, username, email);
}

class _UserNavigationScreenState extends State<UserNavigationScreen> {
  int? uid;
  String? name, username, email;
  _UserNavigationScreenState(uid, name, username, email);
  int currentIndex = 0;
  @override
  Widget build(BuildContext context) {
    print(uid);//this value is null
    print(name);//this value is also null
    final screens = [
      UserPostsScreen(
          uid: uid!, name: name!, username: username!, email: email!),
      //UserAlbumScreen(),
      UserPhotosScreen(),
      //UserTodosScreen(),
    ];

This is from where I want to pass the values and am successfully printing those values

InkWell(
                  onTap: () {
                    print(usersJson[index].id!);//this value is what I want to pass
                    print(usersJson[index].name!);
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => UserNavigationScreen(
                              uid: usersJson[index].id!,
                              name: usersJson[index].name!,
                              username: usersJson[index].username!,
                              email: usersJson[index].email!)),
                    );
                  },

Solution

There is no need to create a Constructor for your State for that. You can use the widget property inside the State like: widget.name or widget.username etc

Answered By – Felipe Morschel

Answer Checked By – Laura B. (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published