【问题标题】:Failed Assertion: Boolean expression must not be null断言失败:布尔表达式不能为空
【发布时间】:2019-05-21 11:25:05
【问题描述】:

我从后端存储了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);
    }
  }

【问题讨论】:

    标签: dart flutter flutter-dependencies


    【解决方案1】:

    在调用它来构建您的页面时,您的 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 的值仍在加载(或仍为null)时显示替代小部件(例如CircularProgressIndicator)。

    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),
      ),
    );
    

    【讨论】:

    • 我的缓存值isQuick 为真。有变化吗?
    • 在从后端加载实际值时,您仍然需要提供默认值。一个好的做法通常是显示加载屏幕,直到从后端加载所有必要的数据。
    • 怎么做?我需要每 10 秒调用一次后端 abd 检查isquick 值是否更改..如果更改我需要更新我的本地数据库
    • 我添加到您的方法中。不工作。我也修改了我的问题
    猜你喜欢
    • 2021-06-06
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 2020-07-05
    • 2020-04-23
    • 2021-07-09
    相关资源
    最近更新 更多