3

I am trying to create a Login using Provider pattern. This is what I've tried so far...

First this is my UserRepository:

enum Status { Uninitialized, Authenticated, Authenticating, Unauthenticated }
class UserRepository with ChangeNotifier{
  User user;
  Status _status =Status.Uninitialized;

  Status get status => _status;
  User get authUser => user;

  Future<bool> signIn(String email, String password) async {
    try{
    var body = jsonEncode({
      'email': email,
      'password': password
    });

    var res = await http.post(('http://192.168.178.35:8000/auth/login'),
        body: body,
        headers: {
          "Accept": "application/json",
          "content-type": "application/json"
        });
    var response = jsonDecode(res.body);

    if(response.status == 200)
    {
      notifyListeners();
      return true;
    }
    }
    catch(e)
    {
      _status = Status.Unauthenticated;
      notifyListeners();
      return false;
    }
  }
}

And this should be my entry page (HomePage):

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<UserRepository>(     
    child: Consumer(
        builder: (context, UserRepository user, _) {
          switch (user.status) {
            case Status.Uninitialized:
              return Login();
            case Status.Unauthenticated:
            case Status.Authenticating:
              return Login();
            case Status.Authenticated:
              return DriverDashboard(user: user.user);
          }
        },
      ),
    );
  }
}

And after I try to run the app I get this error:

enter image description here

Does anyone have any idea why is this happening? Is this correct way how to implement ChangeNotifierProvider?

I am trying to follow this tutorial https://medium.com/flutter-community/flutter-firebase-login-using-provider-package-54ee4e5083c7 ...

However, except Firebase I use custom created RESTful API as a backend.

1 Answer 1

2

Although, this error message is not intuitive, I guess you might return a null in the builder function.

    class HomePage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return ChangeNotifierProvider<UserRepository>(     
        child: Consumer(
            builder: (context, UserRepository user, _) {
              //NOTICE HERE: What If your user is null?
              switch (user.status) {
                case Status.Uninitialized:
                  return Login();
                case Status.Unauthenticated:
                case Status.Authenticating:
                  return Login();
                case Status.Authenticated:
                  return DriverDashboard(user: user.user);
              }
              // return SplashPageHere();
            },
          ),
        );
      }
    }

You should give a splash page, or something that return Widget for builder.

Edit: Here is the example to use ChangeNotifierProvider

    ChangeNotifierProvider<AppState>(
              builder: (_) => AppState(),
              child: MyHomePage(),
            ));

And this is the code you post from medium:

      @override
      Widget build(BuildContext context) {
        return ChangeNotifierProvider(
          builder: (_) => UserRepository.instance(),
          child: Consumer(
            builder: (context, UserRepository user, _) {
              switch (user.status) {
                case Status.Uninitialized:
                  return Splash();
                case Status.Unauthenticated:
                case Status.Authenticating:
                  return LoginPage();
                case Status.Authenticated:
                  return UserInfoPage(user: user.user);
              }
            },
          ),
        );
      }

ChangeNotifierProvider should contain a builder and child, That's why you always have null for your user...

5
  • But my user is always null... I tried to set it from response in UserRepository, but somehow it is always null :(
    – harunB10
    Jul 7, 2019 at 22:24
  • So what I mean is you should return a page to prevent return null in builder function. Just like user == null then return such a page. I didn't target on sloving your logic of code, but errors.
    – Tokenyet
    Jul 8, 2019 at 4:28
  • Now I know, you just need to see the tutorial you post carefully.
    – Tokenyet
    Jul 8, 2019 at 4:45
  • It goes to Dashboard but user is still null. I tried to add it in signIn function like this.user = User.fromJson(json.decode(res.body)); but without success...
    – harunB10
    Jul 8, 2019 at 11:27
  • You could add a breakpoint to see what happened in debug mode. It might be failed to decode, and user is always null. I think you should learn more on debugging to slove these problems.
    – Tokenyet
    Jul 8, 2019 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.