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:
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:
@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() 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), () {});
....
}