布尔表达式不能为null? [英] Boolean expression must not be null?

查看:198
本文介绍了布尔表达式不能为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道我为什么得到Failed断言:布尔表达式不能为null。
如果我已经登录并退出该应用程序,然后再次打开该应用程序,则应该直接在主屏幕中。

Does anyone know why i got the Failed assertion: boolean expression must not be null. If I had logged in and quit the app and open the app again i should be directly in the homescreen.

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

class MyApp extends StatefulWidget {

  // This widget is the root of your application.
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  bool userIsLoggedIn = false;

  @override
  void initState(){
    getLoggedInState();
    super.initState();
  }

  getLoggedInState()async{
    await HelperFunction.getUserLoggedInSharedPreference().then((value){
      setState(() {
        userIsLoggedIn = value;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: userIsLoggedIn ? HomeScreen() : CompanyLoadingBar(),
    );
  }
}


推荐答案

如果您的值在此处可能为空-您会收到错误消息。
为了安全起见,将setState内的行更改为:

If your 'value' here can become null - you get the error. To be safe change the line inside setState to:

userIsLoggedIn = value ?? false; 

如果值为null,它将布尔值设置为false

it will set the bool to false if value is null

这篇关于布尔表达式不能为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆