Provider is not working when navigate to new screen

Viewed 216

I implemented Authentication by provider

The problem is when is the first time myHomeCalss is notified that the user is Authenticated by dont return the correctPage (MainGui)

SplashPages is page with a button continue, and push the login page ,

The Login page is pushed outside of costumer

image

but when I dont pass in the SplashPages is worked perfectyl

any adea please

//splash page

ContinueButton(
                  onPressed:  (){

                      Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (_) =>
                              ListenableProvider.value(
                                value: yourModel,
                                child: LoginPage(),
                              ),
                        ),
                      );
                    }

                )

//main

void main() async {
      setupLocator();
      WidgetsFlutterBinding.ensureInitialized();
      await firebase_core.Firebase.initializeApp();
      runApp(
        MultiProvider(
          providers: [
            ChangeNotifierProvider(create: (_) => AuthenticationService()),
          ],
          child: MyApp(),
        ),
      );
    }

//My app

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: MyHome(),
        builder: (context, child) => Navigator(
              key: locator<DialogService>().dialogNavigationKey,
              onGenerateRoute: (settings) => MaterialPageRoute(
                  builder: (context) => DialogManager(child: child)),
            ));
  }
}

MyHome

 Class MyHome extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return SafeArea(
          child: FutureBuilder<bool>(
              future: startTime(),
              builder: (BuildContext context, AsyncSnapshot<bool> snapshot2) {
                if (snapshot2.hasData) {
                  if (snapshot2.data) {
                    return SplashPages();
                  } else {
                    return Consumer<AuthenticationService>(builder: (_, auth, __) {
                      if (auth.currentUserr == null) {
                        return LoginPage();
                      } else {
                        return FutureBuilder(
                            future: auth.populateCurrentUser(auth.currentUserr),
                            builder: (context, snapshot) {
                              if (snapshot.hasData) {
                                if (auth.currentUserr.emailVerified) {
                                  return MainGui();
                                } else {
                                  return ValidationMailPage(
                                    email: auth.currentUserr.email,
                                  );
                                }
                              } else
                                return Container(
                                    //     child: Center(
                                    //         child: SpinKitRotatingCircle(
                                    //   color: Colors.white,
                                    //   size: 50.0,
                                    // ))
                                    );
                            });
                      }
                    });
                  }
                } 
1 Answers

You may consider using SharedPreferences, in which you will store the user (or maybe just the token), and then check in main if there is a token/user stored there before rendering the app; if there is a token you log in and then push to the homepage, if not you navigate directly to the login page.

SharedPrefenreces is persisted data storage that persists even if you restart the app, but Provider is a state management solution that doesn't persist between app restarts.

Here is the SharedPreferences plugin you may use.

Related