Flutter :: change value in initialstate in statefull widget from future list

536 Views Asked by At

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.

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?

1

There are 1 best solutions below

2
On BEST ANSWER

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:

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