[英]Shared preferences is not working during login in flutter
Below is my code for splash screen where I have used shared preferences.下面是我使用共享首选项的启动画面代码。 in this when user navigates to login i am checking for useremail whether user is already logged in or not.If user is already logged in it should navigate to home page directly if not it should return to login page after splash screen.But in this case shared preferences is not working can anyone guid me what's wrong in this code:
在这种情况下,当用户导航到登录时,我正在检查 useremail 是否用户已经登录。如果用户已经登录,它应该直接导航到主页,如果没有,它应该在闪屏后返回登录页面。但在这种情况下共享首选项不起作用任何人都可以指导我这段代码有什么问题:
import 'package:flutter/material.dart';
import 'package:onesignal_flutter/onesignal_flutter.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'View/LoginPage.dart';
import 'View/homePageAdmin.dart';
class SplashScreen extends StatefulWidget {
@override
State<SplashScreen> createState() => _SplashScreenState();
}
class SharedPrefService {
static SharedPreferences? pref; // nullable variable
static Future<void> init() async {
await SharedPrefService.init();
SharedPrefService.pref = await SharedPreferences.getInstance();
}
}
class _SplashScreenState extends State<SplashScreen> {
String playerId = '';
@override
void initState() {
super.initState();
initPlatformState();
_navigateToHome();
}
Future<void> initPlatformState() async {
OneSignal.shared.setAppId('e89acaa4-5388-4e3a-bd69-44d197bdcbd7');
final status = await OneSignal.shared.getDeviceState();
final String? osUserID = status?.userId;
print('The player id from main.dart ...........${osUserID}');
setState(() {
this.playerId = osUserID!;
});
print('this.playerid from main.dart ${this.playerId}');
}
_navigateToHome() async {
String pId = this.playerId;
var usrname = SharedPrefService.pref?.getString('username');
var usrEmail = SharedPrefService.pref?.getString('email');
await Future.delayed(Duration(milliseconds: 1500), () {});
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => usrEmail == null
? LoginPage()
: homePageAdmin(
pId,
usrname.toString(),
),
));
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.orangeAccent,
body: Center(
child: Container(
decoration: BoxDecoration(
color: Colors.white, borderRadius: BorderRadius.circular(5)),
height: MediaQuery.of(context).size.height * 0.13,
width: MediaQuery.of(context).size.width * 0.5,
child: Image(image: AssetImage('assets/image/AtDocHublogo.png'))),
));
}
}
You need to await
for initPlatformState
result, but because you call it in initState
you can't use await
, so one thing you can do is call _navigateToHome
inside initPlatformState
like this:您需要
await
initPlatformState
结果,但是因为您在initState
中调用它,所以您不能使用await
,所以您可以做的一件事是在initPlatformState
中调用_navigateToHome
,如下所示:
@override
void initState() {
super.initState();
initPlatformState();
//remove _navigateToHome from here
}
Future<void> initPlatformState() async {
OneSignal.shared.setAppId('e89acaa4-5388-4e3a-bd69-44d197bdcbd7');
final status = await OneSignal.shared.getDeviceState();
final String? osUserID = status?.userId;
print('The player id from main.dart ...........${osUserID}');
this.playerId = osUserID!; // <--- change this
print('this.playerid from main.dart ${this.playerId}');
_navigateToHome(); // <--- add this this
}
also you need to call SharedPrefService's init
inside _navigateToHome
like this:您还需要像这样在
_navigateToHome
中调用 SharedPrefService 的init
:
_navigateToHome() async {
String pId = this.playerId;
await SharedPrefService.init(); // <--- add this
var usrname = SharedPrefService.pref?.getString('username');
var usrEmail = SharedPrefService.pref?.getString('email');
await Future.delayed(Duration(milliseconds: 1500), () {});
....
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.