skip to Main Content

here is my code,

class ProfileOthers extends StatefulWidget {
  const ProfileOthers({super.key, required this.data});

  final Map<String, dynamic> data; //I am hoping to make use of this data

  @override
  State<ProfileOthers> createState() => _ProfileOthersState();
}

Want to pass arguments in stateless widget from another screen, I’m new in flutter.

3

Answers


  1. one simple way to access variables passed through initializing that widget within the build method would be using

        widget.data
    

    where you want like displaying it within a string.

    Using the code you provided you could write something like this:

    class ProfileOthers extends StatefulWidget {
        const ProfileOthers({super.key, required this.data});
    
        final Map<String, dynamic> data; 
    
        @override
        State<ProfileOthers> createState() => _ProfileOthersState();
    }
    
    class _ProfileOthersState extends State<ProfileOthers> {
       @override
       Widget build(BuildContext context) { 
           return Scaffold(
               body: Container(
                  child: Text(
                     '${widget.data['Sample key value']}'
                  ),
               ),
           );
       }
    }
    
    Login or Signup to reply.
  2. class UserProfileScreen extends StatefulWidget {
       final Map<String, dynamic> userData;
       const UserProfileScreen ({super.key, required this.userData});
    
      @override
      State<UserProfileScreen> createState() => _UserProfileScreenState();
    }
    

    you can access this parameter as shown below,

    Text(widget.userData['name']),
    
    Login or Signup to reply.
  3. Text(widget.data['key value']),
    

    use this parameter

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search