Provider is not working when navigate to new screen

166

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.

Share:
166
Fatima ayaa
Author by

Fatima ayaa

Updated on December 29, 2022

Comments

  • Fatima ayaa
    Fatima ayaa over 1 year

    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,
                                        // ))
                                        );
                                });
                          }
                        });
                      }
                    } 
    
    • Sisir
      Sisir about 3 years
      So whats the problem that you are facing?
    • Fatima ayaa
      Fatima ayaa about 3 years
      the problem is when passing to splashes-> Login (autheniticated its worked but its not redirect again to MainGui°
    • Sisir
      Sisir about 3 years
      So after the login is done it doesn't navigate to home page?
    • Fatima ayaa
      Fatima ayaa about 3 years
      yes thas it espacilly when i pass by splash pages
    • Fatima ayaa
      Fatima ayaa about 3 years
      the image is show that loginPage is outside of consumer
    • Sisir
      Sisir about 3 years
      Why do you need a provider in this case? Can't you use a SharedPref to store the first time loing and always check if the user is authenticated or not and based on that showing the login or home page?
    • Fatima ayaa
      Fatima ayaa about 3 years
      yes thats what i did , the senario, is when isthefirt instalation = > splashesPages- push LoginVien, if is logged , MainGui , if isNOt the firstInslation => LoginPage
    • Sisir
      Sisir about 3 years
    • Sisir
      Sisir about 3 years
      As discussed, you can change your design as suggested in this SO post: stackoverflow.com/questions/54377188/…
  • Fatima ayaa
    Fatima ayaa about 3 years
    thanks for comment but, i want the solution with provider :)
  • Hamza Mogni
    Hamza Mogni about 3 years
    you can't do that, provider will never persist the state. Provider will be the layer between SharedPreferences and your UI. I mean, Provider will get the data from SharedPreferences and then provide it to your UI.