gpt4 book ai didi

flutter : SharedPreferences not fetching value at app start

转载 作者:IT王子 更新时间:2023-10-29 07:16:27 27 4
gpt4 key购买 nike

我正在尝试存储一个值,并根据该值导航到 LandinPage 或 HomePage。但是,当我的应用程序加载时,我无法获得 SharedPreferences 值。目前,该值是在单击着陆页中的按钮以及关闭/最小化应用程序时设置的。我什至看不到来自 main.dart 的打印消息,也无法获取值。我究竟做错了什么?

这是我的代码:

import 'package:credit/src/pages/landing.dart';
import 'package:flutter/material.dart';
import 'package:credit/src/pages/credit/home.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
MyApp({Key key}) : super(key: key);

_LoadingPageState createState() => _LoadingPageState();
}

class _LoadingPageState extends State<MyApp> {
@override
void initState() {
super.initState();
getUserStatus().then((userStatus) {
if (userStatus == null) {
Navigator.of(context)
.push(MaterialPageRoute<Null>(builder: (BuildContext context) {
return LandingPage();
}));
} else {
Navigator.of(context)
.push(MaterialPageRoute<Null>(builder: (BuildContext context) {
return HomePage();
}));
}
});
}

@override
Widget build(BuildContext context) {
return Container(
child: Center(
child: CircularProgressIndicator(),
));
}
}

Future<String> getUserStatus() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String userStatus = prefs.getString('userstatus');
print("==On Load Check ==");
print(userStatus);
return userStatus;
}

最佳答案

您可能需要使用在您的两个页面中的任何一个之前首先加载的“加载页面”:

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'An App',
home: LoadingPage(),
routes: {
'/landing': (context) => LandingPage(),
'/home': (context) => HomePage(),
}
);
}
}


class LoadingPage extends StatefulWidget {
LoadingPage({Key key}) : super(key: key);

_LoadingPageState createState() => _LoadingPageState();
}

class _LoadingPageState extends State<LoadingPage> {
@override
void initState() {
super.initState();
loadPage();
}

loadPage() {
getUserStatus().then((userStatus) {
if (userStatus == null) {
Navigator.of(context).pushNamed('/landing');
} else {
Navigator.of(context).pushNamed('/home');
}
});
}

@override
Widget build(BuildContext context) {
return Container(
child: Center(
child: CircularProgressIndicator(),
));
}
}

class HomePage extends StatefulWidget {
HomePage({Key key}) : super(key: key);

_HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Container(
child: Text('Home Page'),
);
}
}

class LandingPage extends StatefulWidget {
LandingPage({Key key}) : super(key: key);

_LandingPageState createState() => _LandingPageState();
}


class _LandingPageState extends State<LandingPage> {

@override
void initState() {
super.initState();
setUserStatus('done');
}

@override
Widget build(BuildContext context) {
return Container(
child: Text('Landing'),
);
}
}

Future<String> getUserStatus() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String userStatus = prefs.getString('userStatus');
print("==On Load Check ==");
print(userStatus);
return userStatus;
}

Future<bool> setUserStatus(String userStatus) async{
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('userStatus', userStatus);
return true;
}

关于 flutter : SharedPreferences not fetching value at app start,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57235319/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com