0

I'm creating a simple app where user can be authenticated using firebase google_sigin. I have created AuthChecker widget which checks the authentication and returns login page, where user can login with google. After when the login in complete I want to go back to AuthChecker and show homepage.

Following code below is my implementation which gives context error

From the main.dart file the AuthChecker widget is called:

class AuthChecker extends StatefulWidget {
  @override
  _AuthCheckerState createState() => _AuthCheckerState();
}

class _AuthCheckerState extends State<AuthChecker> {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
        future: getCurrentUser(),
        builder: (context, snapshot){
          if(snapshot.connectionState == ConnectionState.done){
            //got response
            FirebaseUser user = snapshot.data;
            LoginStateProvider prov = Provider.of<LoginStateProvider>(context);
            if(user == null){
              //not loggedin pls login
              return LoginPage();
            }else{
              //already logged in
              print("Already logged in");
              prov.updateUserState(user);
              return Home();
            }
          }
        },
      );
  }
}

The login page contains the signin button:

Widget signInButton(BuildContext context) {
    return OutlineButton(
      onPressed: () {
        signInWithGoogle().whenComplete(() {
        Navigator.of(context).pushAndRemoveUntil(
          MaterialPageRoute(
            builder: (context)=>AuthChecker();
          ),
          ModalRoute.withName('/auth_check')
        );
      },
}

The provider class looks like:

class LoginStateProvider with ChangeNotifier{
  String uid;
  String name;
  String email;
  String profilePhoto;

  LoginStateProvider({
    this.uid,
    this.name,
    this.email,
    this.profilePhoto,
  });

  void updateUserState(FirebaseUser user){
    this.uid = user.uid;
    this.name = user.displayName;
    this.email = user.email;
    this.profilePhoto = user.photoUrl;
  }

}

I got following error:

I/flutter ( 9551):   * Ensure the Provider<LoginStateProvider> is an ancestor to this FutureBuilder<FirebaseUser>
I/flutter ( 9551): Widget
I/flutter ( 9551):   * Provide types to Provider<LoginStateProvider>
I/flutter ( 9551):   * Provide types to Consumer<LoginStateProvider>
I/flutter ( 9551):   * Provide types to Provider.of<LoginStateProvider>()
I/flutter ( 9551):   * Always use package imports. Ex: `import 'package:my_app/my_code.dart';
I/flutter ( 9551):   * Ensure the correct `context` is being used.
I/flutter ( 9551):
I/flutter ( 9551): If none of these solutions work, please file a bug at:
I/flutter ( 9551): https://github.com/rrousselGit/provider/issues
Abishek Bashyal
  • 307
  • 1
  • 6
  • 15

1 Answers1

0

you could try and do this:

Widget signInButton(BuildContext context) {
    return OutlineButton(
      onPressed: () {
        signInWithGoogle().whenComplete(() {
        FirebaseUser user = snapshot.data;
        if (user == null) {
         //Route to login
        } else {
         //route to somewhere
        }
      },
}

and create a splash screen with your logo and a async function that runs at initState that checks if user == null and route to login if needed or to homepage if already logged in.

there's a nice example here: Firebase Auth state check in Flutter

I'm not sure if it helps or if that was really your question, sorry if I misunderstood u

Fernando Rocha
  • 2,416
  • 3
  • 15
  • 28