0

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 Answers1

0

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();
  }
tareq albeesh
  • 1,701
  • 2
  • 10
  • 13
  • it give me the value has not been initialized – Sarmed MQ Berwari Apr 05 '22 at 21:07
  • that's because you have to initialize `value` with initial value since it would need to wait for a future function to be updated, you can solve it by defining your `value` variable like: `double value = 0;` or by making it `late` which would make a problem depending on your builder content. – tareq albeesh Apr 05 '22 at 21:18