skip to Main Content

I’m trying to update data of provider and use them in the next page, but the data is going to be null.

Here is my model and provider:

class User {
  String? firstName;
  String? lastName;

  User({this.firstName, this.lastName});

  update({String? firstName, String? lastName}) {
    this.firstName = firstName ?? this.firstName;
    this.lastName  = lastName  ?? this.lastName;
  }
}
class UserProvider with ChangeNotifier {
  late User user;

  void updateUser({String? firstName, String? lastName}) {
    user.update(firstName : firstName, lastName : lastName);
    notifyListeners();
  }
}

And this is the page which I use updateUser method written above:

class _FirstPageState extends State<FirstPage> {
  ...
  Future<void> updateUser(UserProvider provider) async {
    provider.updateUser(
      firstName : _firstNameController!.text,
      lastName  : _lastNameController!.text
    );
  }
  ...
  @override
  Widget build(BuildContext context) {
    final UserProvider userProvider = Provider.of(context, listen : true);
    ...
    MyButton(
      label     : 'Submit',
      onPressed : () async {
        updateUser(userProvider)
          .then((_) => Navigator.pushNamed(context, '/second_page');
      }
    )
    ...

Then this is where I use it:

class SecondPage exntends StatelessWidget {
  ...
  @override
  Widget build(BuildContext context) {
    final UserProvider userProvider = Provider.of(context, listen : true);
    final User user = userProvider.user!;
    print('user.firstName : ${user.firstName}'); // this is for check if it exists
    ...

(These are simplified for ease of thinking, so there might be some mistakes, but please don’t mind.)

Why is it still null? Please tell me how can I fix it.

2

Answers


  1. You missed to initialise user in UserProvider.

    UserProvider should be look like..

    class UserProvider with ChangeNotifier {
      User user = User();
    
      void updateUser({String? firstName, String? lastName}) {
        user.update(firstName : firstName, lastName : lastName);
        notifyListeners();
      }
    }
    

    Or if you are make sure you called updateUser before the usage of user, then you code should be look like…

    class UserProvider with ChangeNotifier {
      late User user;
    
      void updateUser({String? firstName, String? lastName}) {
        user = User(firstName : firstName, lastName : lastName);
        notifyListeners();
      }
    }
    

    Edited

    Introduce one more function to set the initial data for user.

    class UserProvider with ChangeNotifier {
      late User user;
    
      void updateUser({String? firstName, String? lastName}) {
        user.update(firstName : firstName, lastName : lastName);
        notifyListeners();
      }
    
      void setUser(User user){
        this.user = user;
        notifyListeners();
      }
    }
    
    Login or Signup to reply.
  2. Update updateUser method in UserProvider class like below.

     void updateUser({String? firstName, String? lastName}) {
        user.firstName = firstName;
        user.lastName = lastName;
        notifyListeners();
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search