本文介绍了如何解决Flutter中的“断言失败:布尔表达式不能为空”异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Flutter应用程序,并在logcat中不断收到此错误字符串。

I was working on a Flutter application and kept receiving this error String in the logcat.

Failed assertion: boolean expression must not be null

这是有问题的代码:

@override
Widget build(BuildContext context) {
    return Center(
        child: ListView(
            children: <Widget>[
                TextField(
                    controller: controller,
                    decoration: InputDecoration(
                        hintText: "Type in something..."
                    ),
                ),
                RaisedButton(
                    child: Text("Submit"),
                    onPressed: () => addString(),
                ),
                Flex(
                    direction: Axis.vertical,
                    children: (list_two = null) ? [] :
                    list_two.map((String s) => Text(s)).toList() 
                )
            ],
        ),
    );
}

是什么引起了问题?

推荐答案

解决方案很简单,此行在这里:

The solution was a simple one, this line here:

            Flex(
                ...
                children: (list_two = null) ? [] :
                ...
            )

需要让孩子比较为布尔值,需要2个等号。

Needed to have the children comparison be a boolean, which requires 2 equals signs.

            Flex(
                ...
                children: (list_two == null) ? [] :
                ...
            )

使用Android Studio并用Java编写时,通常会引发编译器错误并且无法运行,但是在使用Flutter插件(1.0截至今天,2018-06-26)进行dart编写时,未显示任何编译器错误,而我们看到了运行时错误。

While using Android studio and writing in Java, this would normally throw a compiler error and not run, but while writing in dart with the Flutter plugin (1.0 as of today, 2018-06-26) no compiler error is shown and we instead see a runtime error.

这篇关于如何解决Flutter中的“断言失败:布尔表达式不能为空”异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 05:12