Provider is not working when navigate to new screen
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.
Fatima ayaa
Updated on December 29, 2022Comments
-
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
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 about 3 yearsSo whats the problem that you are facing?
-
Fatima ayaa about 3 yearsthe problem is when passing to splashes-> Login (autheniticated its worked but its not redirect again to MainGui°
-
Sisir about 3 yearsSo after the login is done it doesn't navigate to home page?
-
Fatima ayaa about 3 yearsyes thas it espacilly when i pass by splash pages
-
Fatima ayaa about 3 yearsthe image is show that loginPage is outside of consumer
-
Sisir about 3 yearsWhy 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 about 3 yearsyes thats what i did , the senario, is when isthefirt instalation = > splashesPages- push LoginVien, if is logged , MainGui , if isNOt the firstInslation => LoginPage
-
Sisir about 3 yearsLet us continue this discussion in chat.
-
Sisir about 3 yearsAs discussed, you can change your design as suggested in this SO post: stackoverflow.com/questions/54377188/…
-
-
Fatima ayaa about 3 yearsthanks for comment but, i want the solution with provider :)
-
Hamza Mogni about 3 yearsyou 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.