I'm trying to use shared preference to keep user login and I put it in the splash screen and when I run the project this error has appeared:
[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception:
setState() called after dispose(): _SplashScreenState#aa9c8(lifecycle
state: defunct) This error happens if you call setState() on a State
object for a widget that no longer appears in the widget tree (e.g.,
whose parent widget no longer includes the widget in its build). This
error can occur when code calls setState() from a timer or an
animation callback. The preferred solution is to cancel the timer or
stop listening to the animation in the dispose() callback. Another
solution is to check the "mounted" property of this object before
calling setState() to ensure the object is still in the tree. This
error might indicate a memory leak if setState() is being called
because another object is retaining a reference to this State object
after it has been removed from the tree. To avoid memory leaks,
consider breaking the reference to this object during dispose().
And the data loop doesn't stop calling itself again and again.
This my method to using shared preference:
bool isLoading = true;
init() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool isLog = prefs.getBool("islog");
if (isLog == true) {
String email = prefs.getString("email");
String pass = prefs.getString("pass");
setState(() {
signIn(email, pass);
});
} else {
setState(() {
isLoading = false;
});
}
}
signIn(String email, String pass) async {
var res = await userProvider.login(email, pass);
var user = userProvider.user.tourist;
if (res is FailedRequest) {
Dialogs.showErrorDialog(context, message: res.message, code: res.code);
} else if (user == true) {
print("Signing in success");
await appProvider.countryList();
setState(() {
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (context) => BottomScreen()));
});
}
userProvider.isLoading = false;
setState(() {
isLoading = false;
});
}
this is my splash screen which contain the previous code of shared preference
@override
Widget build(BuildContext context) {
userProvider = Provider.of<UserProvider>(context, listen: false);
appProvider = Provider.of<AppProvider>(context, listen: false);
init();
return isLoading == true
? Container(
color: Colors.white,
child: Center(
child: CircularProgressIndicator()),
)
: Container(
child: Scaffold(
body: Stack(
children: <Widget>[
Container(
foregroundDecoration: !AppTheme.isLightTheme
? BoxDecoration(
color: AppTheme.getTheme()
.backgroundColor
.withOpacity(0.4))
: null,
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: Image.asset('assets/images/introduction.jpg',
fit: BoxFit.cover),
),
Column(
children: <Widget>[
Expanded(
flex: 1,
child: SizedBox(),
),
Center(
child: Container(
width: 60,
height: 60,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(8.0),
),
boxShadow: <BoxShadow>[
BoxShadow(
color: AppTheme.getTheme().dividerColor,
offset: Offset(1.1, 1.1),
blurRadius: 10.0),
],
),
child: ClipRRect(
borderRadius: BorderRadius.all(
Radius.circular(8.0),
),
child: Image.asset('assets/images/appIcon.png'),
),
),
),
SizedBox(
height: 16,
),
Text(
"Voyager",
textAlign: TextAlign.left,
style: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 24,
),
),
SizedBox(
height: 8,
),
Text(
"Best Trips deals for your holiday",
textAlign: TextAlign.left,
style: TextStyle(
fontSize: 14,
),
),
Expanded(
flex: 4,
child: SizedBox(),
),
Padding(
padding: const EdgeInsets.only(
left: 48, right: 48, bottom: 8, top: 8),
child: Container(
height: 48,
decoration: BoxDecoration(
color: AppTheme.getTheme().primaryColor,
borderRadius:
BorderRadius.all(Radius.circular(24.0)),
boxShadow: <BoxShadow>[
BoxShadow(
color: AppTheme.getTheme().dividerColor,
blurRadius: 8,
offset: Offset(4, 4),
),
],
),
child: Material(
color: Colors.transparent,
child: InkWell(
borderRadius:
BorderRadius.all(Radius.circular(24.0)),
highlightColor: Colors.transparent,
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
IntroductionScreen()),
);
},
child: Center(
child: Text(
"Get started",
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 16,
color: Colors.white),
),
),
),
),
),
),
Padding(
padding: EdgeInsets.only(
bottom:
24.0 + MediaQuery.of(context).padding.bottom,
top: 16),
child: Container(
child: Material(
color: Colors.transparent,
child: InkWell(
borderRadius:
BorderRadius.all(Radius.circular(24.0)),
highlightColor: Colors.transparent,
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
LoginScreen(context)),
);
},
child: Text(
"Already have account? LogIn",
textAlign: TextAlign.left,
style: TextStyle(
fontSize: 14,
color: Colors.white,
),
),
),
),
),
),
],
),
],
),
),
);
}
So can anyone help me please with my issue?