flutter - Flutter 在发布模式下卡在启动画面,在调试模式下工作正常
问题描述
我制作了自己的启动画面,并在其中调用了一个函数,该函数_navigate
决定用户下一步应该导航到哪里,主屏幕或身份验证屏幕。
问题是它在调试模式下工作正常导航根据指定的条件正确发生,但在发布模式下它卡住并且根本不导航。
这是代码:
class _SplashScreenState extends State<SplashScreen> {
bool _firstTime = true;
Future<void> _navigate(BuildContext context) async {
bool _isLoggedIn = await Provider.of<Info>(context, listen: false).token();
if (_isLoggedIn) {
final _prefs = await SharedPreferences.getInstance();
User _user = User(
id: _prefs.getString("id"),
name: _prefs.getString("name"),
phoneNumber: _prefs.getInt("phoneNumber"),
);
Provider.of<Info>(context, listen: false).setUser(user: _user);
Navigator.of(context).pushReplacementNamed(HomeScreen.routeName);
} else {
Navigator.of(context).pushReplacementNamed(AuthScreen.routeName);
}
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
if (_firstTime) {
_navigate(context);
_firstTime = false;
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.lightGreen,
body: Center(
child: Container(
padding: EdgeInsets.all(25),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.white,
border: Border.all(
color: Colors.lightGreen,
),
),
clipBehavior: Clip.hardEdge,
child: Image(
height: 200,
width: 200,
image: AssetImage("assets/images/freelance.png"),
),
),
),
);
}
}
这是token
功能:
Future<bool> token() async {
final _prefs = await SharedPreferences.getInstance();
_token = _prefs.getString("token");
return _token != null;
}
和setUser
功能:
void setUser({
@required User user,
}) {
_user = user;
}
解决方案
推荐阅读
- internet-explorer - 为什么以太网层地址有 48 位?
- machine-learning - 学习率如何影响梯度下降?
- docker - Jenkins 多次构建 docker 端口冲突
- java - 如何在 javafx 的主程序中添加启动画面?
- python - Python分割数据记录
- java - 如果 Java 中的类具有私有 mutator 方法,它可以是不可变的吗?
- asp.net-web-api - 检查不记名令牌
- javascript - 如何使用鼠标旋转 3D 对象而不使用three.js 将其旋转到屏幕外?
- java - 使用 Selenium Webdriver 关闭警报
- angular - Selenium net.serenity.bdd.core.exceptions.SerenityManagedException 元素超时