Solution 1 :
You will have to store this data in the users device.
The shared_preferences
package is very helpful for this.
For storing the data:
SharedPreferences perf = await SharedPreferences.getInstance();
perf.setBool(label, value);
For reading the data:
SharedPreferences perf = await SharedPreferences.getInstance();
bool isUserSignedIn = perf.getBool(label);
Here label
is a String
which is a unique identifer
For more information on the shared_preferences
package use this link
https://pub.dev/packages/shared_preferences/example
Solution 2 :
Create a file called ROOTPAGE.dart and add the code to check if there is an existing user logen in. If a user id is found then go to home page else go to login,
import 'package:name/pages/homepage.dart';
import 'package:name/pages/login_page.dart';
import 'package:flutter/material.dart';
import 'package:name/services/authentication.dart';
enum AuthStatus {
NOT_DETERMINED,
NOT_LOGGED_IN,
LOGGED_IN,
}
class RootPage extends StatefulWidget {
RootPage({this.auth});
final BaseAuth auth;
@override
State<StatefulWidget> createState() => new _RootPageState();
}
class _RootPageState extends State<RootPage> {
AuthStatus authStatus = AuthStatus.NOT_DETERMINED;
String _userId = "";
@override
void initState() {
super.initState();
widget.auth.getCurrentUser().then((user) {
setState(() {
if (user != null) {
_userId = user?.uid;
}
authStatus =
user?.uid == null ? AuthStatus.NOT_LOGGED_IN : AuthStatus.LOGGED_IN;
});
});
}
void loginCallback() {
widget.auth.getCurrentUser().then((user) {
setState(() {
_userId = user.uid.toString();
authStatus = AuthStatus.LOGGED_IN;
Navigator.of(context).pushReplacementNamed('/');
}); });
}
void logoutCallback() {
setState(() {
authStatus = AuthStatus.NOT_LOGGED_IN;
_userId = "";
});
}
Widget buildWaitingScreen() {
return Scaffold(
body: Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
......
),
);
}
@override
Widget build(BuildContext context) {
switch (authStatus) {
case AuthStatus.NOT_DETERMINED:
return buildWaitingScreen();
break;
case AuthStatus.NOT_LOGGED_IN:
return new LoginSignUpPage(
auth: widget.auth,
loginCallback: loginCallback,
title: "name",
);
break;
case AuthStatus.LOGGED_IN:
if (_userId.length > 0 && _userId != null) {
return new HomePage(
userId: _userId,
auth: widget.auth,
logoutCallback: logoutCallback,
);
} else
return buildWaitingScreen();
break;
default:
return buildWaitingScreen();
}
}
}
pass this class to your main.dart
home: new RootPage(auth: new Auth())
I have an auth class which has the login functions. adapt this code to help you solve your problem. Here is the getcurrent user func
Future<FirebaseUser> getCurrentUser() async {
FirebaseUser user = await _firebaseAuth.currentUser();
return user;
}
Problem :
I have created a file for custom button as a widget function. But I need to use it in many places. But I need to route the page where I need at that time. How to achieve this? Please help!
Comments
Comment posted by Xor96
In java, there is a option called private to store the shared preferences in private mode is there any thing like that in flutter?