2

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,
                                    // ))
                                    );
                            });
                      }
                    });
                  }
                } 
Fatima ayaa
  • 610
  • 6
  • 16
  • So whats the problem that you are facing? – Sisir Apr 21 '21 at 09:19
  • the problem is when passing to splashes-> Login (autheniticated its worked but its not redirect again to MainGui° – Fatima ayaa Apr 21 '21 at 09:28
  • So after the login is done it doesn't navigate to home page? – Sisir Apr 21 '21 at 09:30
  • yes thas it espacilly when i pass by splash pages – Fatima ayaa Apr 21 '21 at 09:31
  • the image is show that loginPage is outside of consumer – Fatima ayaa Apr 21 '21 at 09:33
  • 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? – Sisir Apr 21 '21 at 09:40
  • yes thats what i did , the senario, is when isthefirt instalation = > splashesPages- push LoginVien, if is logged , MainGui , if isNOt the firstInslation => LoginPage – Fatima ayaa Apr 21 '21 at 09:42
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/231411/discussion-between-sisir-and-anna). – Sisir Apr 21 '21 at 09:48
  • 2
    As discussed, you can change your design as suggested in this SO post: https://stackoverflow.com/questions/54377188/how-to-use-shared-preferences-to-keep-user-logged-in-flutter/54382116#54382116 – Sisir Apr 21 '21 at 10:15

1 Answers1

1

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.

Hamza Mogni
  • 672
  • 1
  • 7
  • 21
  • thanks for comment but, i want the solution with provider :) – Fatima ayaa Apr 27 '21 at 10:57
  • 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. – Hamza Mogni Apr 27 '21 at 11:32