I was adding login/register in my app using Firebase. It is working perfectly, but now i wanted to check if the user has already registered and logged in when he opens the app, if that's true then show him the Home page, else show him the Login page. So i did some searching and mostly it's done using the Streambuilder method. But all i did was to check if the user is logged in using
initialRoute: FirebaseAuth.instance.currentUser == null
? Routes.loginScreen
: Routes.homePage,
and it works perfectly, Almost everyone is using the Streambuilder method. So i was curiois am i doing it the wrong way? Is there a better way to do this? Here's the full code btw. Which is working fine, but i just want to know if it's the right way?
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _initialization = Firebase.initializeApp();
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: _initialization,
builder: (context, snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
return Center(
child: CircularProgressIndicator(),
//Todo Add splashscreen
);
}
return MaterialApp(
debugShowCheckedModeBanner: false,
title: "MyApp",
initialRoute: FirebaseAuth.instance.currentUser == null
? Routes.loginScreen
: Routes.homePage,
onGenerateRoute: Routes.generateRoute,
);
},
);
}
}