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


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;
      {Key? key,
      required this.uid,
      required this.name,
      required this.username,
      required this.email})
      : super(key: key);
  static const String id = 'user_navigation_screen';

  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;
  Widget build(BuildContext context) {
    print(uid);//this value is null
    print(name);//this value is also null
    final screens = [
          uid: uid!, name: name!, username: username!, email: email!),

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

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


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