Search code examples
flutterdartflutter-provider

How to Store Data Locally Across Multiple Widgets before Pushing to the Database?


To give you some context, I'm building an onboarding page on Flutter where users will be brought first to fill in his username and he will then be pushed to the next widget, date of birth, etc.

I'm planning to store the data locally as the user moves from the first to the last widget and to only push the accumulated data as a class altogether, User(username: xyz, date of birth: 11/11/2001, gender: male, interest: tennis).

I plan to use the Provider package, where I'll store the data locally as shown below before pushing the user to the next page. However, I'm unsure of how to set the individual properties of the _user instance in the Provider class

Can you kindly advise how to go about this? Or if there is a better way to store data locally.

My code snippet for the provider for each page is as shown:

class User {
  String uid;
  String name;
  String email;
  String username;
  String status;
  int state;
  String profilePhoto;

  User({
    this.uid,
    this.name,
    this.email,
    this.username,
    this.status,
    this.state,
    this.profilePhoto,
  });
}

class UserProvider {
  User _user;
  User get getUser => _user;

  set setUsername(username){
    _user.name = username;
  }

  set setEmail(email){
    _user.email = email;

etc...
  }
}


//username page
  Provider.of<UserProvider>(context, listen:false).setUsername='johncena';
  print(Provider.of<UserProvider>(context, listen:false).getUser.username);


//email page
  Provider.of<UserProvider>(context, listen:false).setEmail='hello@gmail.com';
  print(Provider.of<UserProvider>(context, listen:false).getUser.username);

//at the last page
  Provider.of<UserProvider>(context, listen:false).setStatus='available';
  print(Provider.of<UserProvider>(context, listen:false).getUser.status);

//then I'll push the _user instance data that I stored in UserProvider to the backend

However, I'm currently getting this error message as shown below:

[VERBOSE-2:ui_dart_state.cc(177)] Unhandled Exception: NoSuchMethodError: The setter 'username=' was called on null.
Receiver: null
Tried calling: username="johncena"
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1      UserProvider.setUsername= (package:okepos/provider/user_provider.dart:13:11)
#2      _UsernameState.build.<anonymous closure> (package:okepos/screen/Onboarding/username.dart:76:32)
#3      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:993:19)
#4      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:1111:38)
#5      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:183:24)
#6      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:598:11)
#7      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:287:5)
#8      BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:222:7)
#9      PrimaryPointerGes<…>

does anyone know what the issue is?


Solution

  • you should make an instance of User in UserProvider constructor!
    this will solve the error.
    it would be like this:

    class UserProvider {
      User _user;
    
      UserProvider() {
        _user = User();
      }
    
      User get getUser => _user;
    
      set setUsername(username){
        _user.name = username;
      }
    
      set setEmail(email){
        _user.email = email;
    
    etc...
      }
    }
    

    You can also use the User class as provider class directly