dart - 断言失败 : Boolean expression must not be null

标签 dart flutter flutter-dependencies

我从后端存储了 isQuick 值。我使用 sqflite 来缓存。 结果始终为 false

在我的仪表板页面中,

 bool isQuick;
 @override
  void initState() {
 isQuick = false;

 timer1 = Timer.periodic(Duration(seconds: 5), (Timer t) {
  checkQuick(_url, tokens, isQuick);
});

timer = Timer.periodic(Duration(seconds: 10), (Timer t) {
      Future datas = HelperDatabase1().displayGetUserPreference();
      datas.then((v) => {
        data = v,
        print('new data ${data[0].data}'),
        data[0].data == 0 ? this.isQuick == false : this.isQuick == true,
        print(this.isQuick)
      });
      submitRequestSave(_url, tokens);
    });
    }

在我的构建方法中,

 @override
  Widget build(BuildContext context) {
  return WillPopScope(
        onWillPop: () async => false,
        child: Scaffold(
          backgroundColor: Colors.white,
          appBar: AssetRegisterAppBar(context),
          body: makeBody(litems, litems_icon, _url, this.isQuick, isOffline,
              statusbarHeight, context),
        ));
}

checkQuick 方法,

  Future checkQuick(String url, String token, bool isQuick) async {
    print('quick $isQuick');
    bool newQuick;
    final response = await http.get(
      '$url/nativeapi/v1.0/User/GetUserPreference',
      headers: {'Authorization': 'Bearer $token'},
    );
    final jsonResponse = json.decode(response.body);
    GetUserPreference model = GetUserPreference.fromJson(jsonResponse);
    var data = GetUserPreference(data: model.data);
    print(data.data);
    if (data.data == 0) {
      newQuick = false;
    } else {
      newQuick = true;
    }
    print('new quick $newQuick');
    if (isQuick != newQuick) {
      int newData;
      if (newQuick) {
        newData = 1;
      } else {
        newData = 0;
      }
      await HelperDatabase1().updateGetUserPreference(1, newData);
    }
  }

最佳答案

在调用它来构建您的页面时,您的 isQuick 为 null。为其分配默认值:

@override
void initState() {
  isQuick = false; // or true

  Future datas = HelperDatabase1().displayGetUserPreference();
  datas.then((v) =>
    {data = v, data[0].data == 0 ? this.isQuick == false : this.isQuick == true});
}

更新

要解决主要问题,您需要在 isQuick 的值仍在加载(或仍为 ).

return WillPopScope(
  onWillPop: () async => false,
  child: Scaffold(
    backgroundColor: Colors.white,
    appBar: AssetRegisterAppBar(context),
    body: isQuick == null
      ? CircularProgressIndicator()
      : makeBody(litems, litems_icon, _url, this.isQuick, isOffline, statusbarHeight, context),
  ),
);

关于dart - 断言失败 : Boolean expression must not be null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56237234/

相关文章:

flutter - "zsh: command not found: pub"在测试我的代码进行锻炼时

flutter auto_router : Error: Type 'NavigatorState' not found

flutter - 提供者 : "Bad state: Tried to read a provider that threw during the creation of its value"

datatable - 使用 Firestore 和 Flutter 填充 DataTable(使用 StreamBuilder)

dart - 使用 Futures 的 Flutter Like 按钮功能

firebase - 如果电子邮件和密码在 firebase flutter 中无效,如何显示错误消息?

dart - 如何在抽象类中声明工厂构造函数?

swift - Flutter: Firebase_Analytics - 架构 x86_64: "_OBJC_CLASS_$_FIRAnalytics"的 undefined symbol ,引用自:

flutter - 当我在flutter的didChangeDependencies中调用多个提供程序时,显示错误

dart - 如何在 Flutter 中将 PointMode 传递给 Canvas.drawPoints(..)?