首页 > 解决方案 > 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;
  }

标签: flutterdart

解决方案


推荐阅读