i've implemented android application using flutter , and i have created a login/signup form , it works perfect when connected with API but the problem is after i logged in and go back to any other page again when i click on the login button it shows me again the login form , what i want is to add function that detects if user logged in or not , is there a way to do it? ps: i tried sharedPrefrences below but it doesn't work
main() async {
// if your flutter > 1.7.8 : ensure flutter activated
WidgetsFlutterBinding.ensureInitialized();
final SharedPreferences prefs = await SharedPreferences.getInstance();
var isLoggedIn = prefs.getBool('isLoggedIn')??false;
await translator.init(
localeDefault: LocalizationDefaultType.device,
languagesList: <String>['en', 'ar'],
assetsDirectory: 'assets/langs/',
apiKeyGoogle: '<Key>', // NOT YET TESTED
);
// WidgetsFlutterBinding.ensureInitialized();
runApp(
LocalizedApp(
child: Notification(isLoggedIn:isLoggedIn),
),
);
}
class Notification extends StatefulWidget {
final bool isLoggedIn;
Notification({this.isLoggedIn});
@override
_NotificationState createState() => _NotificationState();
}
class _NotificationState extends State<Notification> {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
Products_FitnessAndGym()),
ChangeNotifierProvider.value(value: Cart()),
ChangeNotifierProvider.value(value: Wishe()),
ChangeNotifierProvider.value(
value: Orders(),
)
],
child: MaterialApp(
debugShowCheckedModeBanner: false,
home:
widget.isLoggedIn ? MyHomePage(): LogIn(),
localizationsDelegates:
translator.delegates, /
and this my main.dart:
main() async {
// if your flutter > 1.7.8 : ensure flutter activated
WidgetsFlutterBinding.ensureInitialized();
await translator.init(
localeDefault: LocalizationDefaultType.device,
languagesList: <String>['en', 'ar'],
assetsDirectory: 'assets/langs/',
apiKeyGoogle: '<Key>', // NOT YET TESTED
);
// Future<void> main() async {
// WidgetsFlutterBinding.ensureInitialized();
// final SharedPreferences prefs = await SharedPreferences.getInstance();
// var isLoggedIn = (prefs.getBool('isLoggedIn') == null)
// ? false
// : prefs.getBool('isLoggedIn');
// // runApp(MaterialApp(
// // debugShowCheckedModeBanner: false,
// // home: isLoggedIn ? Home() : LogIn(),
// // ));
// }
runApp(
LocalizedApp(
child: Notification(),
),
);
}
class Notification extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider.value(value: Products_FitnessAndGym()),
ChangeNotifierProvider.value(value: Cart()),
ChangeNotifierProvider.value(value: Wishe()),
ChangeNotifierProvider.value(
value: Orders(),
)
],
child: MaterialApp(
debugShowCheckedModeBanner: false,
home:
isLoggedIn ? MyHomePage(): LogIn(),
localizationsDelegates:
translator.delegates, // Android + iOS Delegates
locale: translator.locale, // Active locale
supportedLocales: translator.locals(),
.........
........