Make a sign in functionality using Riverpod Flutter

1.7k views Asked by At

I am new to river_pod and I won't lie if I said that the documentaion is only for super-senior-engineers

Anyways, I want to implement a sign in feature using this package

The logic behind it is simple:- I get the the email and password from the user and then change the loggedIn state from being false to true

Here is my Provider


    final userAuthProvider = StateProvider<Map<String, bool>>(
        (_) => {'signedIn': false, 'loaded': true, 'signInWasDissmised': false});

And here is how I use it and change the value


    void signIn(BuildContext context, String username, String userPassword) {
        // Check if the username/email and password exist and are correct...etc;
        final String usernameOrEmail = username;
        final String password = userPassword;
    
        // ...logic
    
    context.read(userAuthProvider).state['signedIn'] = true;

I'm using context.read because I'm inside an onPressed of the RaisedButton

Now, what's wrong with this is that after changing the value to be true I don't get flutter to rebuild everything and use the new variable

How can I implement such thing? And am I even doing it right?

1

There are 1 answers

2
Rémi Rousselet On BEST ANSWER

context.read(userAuthProvider).state['signedIn'] = true;

You can't do that. Instead, clone the object:

final auth = context.read(userAuthProvider);

auth.state = {
  ...auth.state,
  'signedIn' = true,
};