简体   繁体   English

Flutter:: 更改未来列表中有状态小部件的初始状态值

[英]Flutter :: change value in initialstate in statefull widget from future list

If I have statefull widget with initial satate variable called value,Like this:如果我有状态小部件,初始状态变量称为值,就像这样:

@override
  void initState() {
    thisDayActivity = dataBase.getDetails(widget.courseId, widget.actId);

    value = 0.0;

    super.initState();
  }

thisDayActivity is future list comming from sqflite database, I want check if the list is empty the value variable equal to 0,else value equale some data in future list. thisDayActivity 是来自 sqflite 数据库的未来列表,我想检查列表是否为空,值变量等于 0,否则值等于未来列表中的一些数据。

I tride this but don't work:我试过这个但不起作用:

@override
  void initState() {
    thisDayActivity = dataBase.getDetails(widget.courseId, widget.actId);
    if (thisDayActivity == []) {
      value = 0.0;
    } else {
      value = thisDayActivity[0]['digree'].toDouble();
    }
    super.initState();
  }

How can I solve this?我该如何解决这个问题?

your method is not working since you are reading a value from a future function, what you need to do is to use the then method to achieve your goal like this:您的方法不起作用,因为您正在读取未来 function 的值,您需要做的是使用then方法来实现您的目标,如下所示:

  @override
  void initState() {
    dataBase.getDetails(widget.courseId, widget.actId)
        .then((thisDayActivity) {
      if (thisDayActivity == []) {
        value = 0.0;
      } else {
        value = thisDayActivity[0]['digree'].toDouble();
      }
    });
    super.initState();
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从另一个小部件更改有状态小部件中变量的值 - How to change value of variable in statefull widget from another widget 从有状态的父级修改有状态的子小部件中的值 - Modify value in statefull child widget from its statefull parent Flutter 中的自定义 StateFull 小部件 - Custom StateFull widget in Flutter 如何在 flutter 中将 id 从有状态小部件获取到无状态小部件? - how to get the id from statefull widget to stateless widget in flutter? Flutter 检测传递给有状态小部件的值何时发生变化 - Flutter detect when value passed to the statefull widget changes 为什么当我在其小部件中调用 Flutter 有状态小部件时,它没有更改另一个有状态小部件的 state - Why Flutter statefull widget didn't change the state of another statefull widget when I call it inside its widget Flutter Statefull 小部件不会从提供程序 package 自动更新 - Flutter Statefull widget not updating automatically from provider package Flutter 将数据从一个 class 传递到另一个(有状态小部件) - Flutter pass data from one class to another(statefull widget) Flutter - 未来<list<widget> > 关于 FutureBuilder </list<widget> - Flutter - Future<List<Widget>> on FutureBuilder 在有状态小部件中按路线名称进行颤动导航 - Flutter navigation by route name in statefull widget
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM