0

i'am trying to get a "Water Meter" going. When i'am opening the App the Value for this should be generated. My problem here is that when i am building the Widget it doesn't Render the actual value. Only if i go to another Screen or Hot Reload the screen it renders out the value. Here i calculate and set the State.

    var dif = parsedDate.difference(dateNow).inDays;

    var difInPercent = double.parse((dif / interval).toStringAsFixed(2));

    setState(() {
      waterPercentage = difInPercent*100;
    });

    print(waterPercentage); // for debug purposes

And later in my Build method i use this Variable i defined globally for my Widget

Container(
     height: waterPercentage,
     width: 15,
[...]

Obviously i'am doing something wrong because the state is not set directly. The Function which runs this set State "plantDate()" gets called in another widget.

Text(
   "Next watering: \n${plantDate(widget.plant.date, widget.plant.interval)}",
[...]

Anyone got an idea ?

EDIT: Here is the Code for the complete Class https://pastebin.com/N4M55YnA

EDIT2: Did it like that but it doesn't work like that.

@override
 initState(){
   super.initState();
   interval = widget.plant.interval;
   parsedDate = DateTime.parse(date);
   date = DateFormat.MMMMd().format(parsedDate);

   var dateNow = DateTime.now();
   var dif = parsedDate.difference(dateNow).inDays;
   var difInPercent = double.parse((dif / interval).toStringAsFixed(2));
   waterPercentage = difInPercent*100;
 }
Subby
  • 1
  • 2
  • Hi! Could you please give the code for your complete class for the widget. Would be better to find the problem. – Shardul Nalegave Jun 23 '20 at 15:47
  • BTW where have you put your `setState` part? In which method is it located? – Shardul Nalegave Jun 23 '20 at 15:48
  • https://pastebin.com/N4M55YnA There you go. Put it on Pastebin because its kinda long – Subby Jun 23 '20 at 15:57
  • I think you should do the work in the `initState` method. You are calling the method only once do it means you want the data to be loaded only once (i.e. at widget build). The better method to do it would be to use the `initState` method. Keep the code same but just set the variable directly and don't use `setState`. – Shardul Nalegave Jun 23 '20 at 16:08
  • Try doing it... If it works I will post it as an answer.. – Shardul Nalegave Jun 23 '20 at 16:08
  • Ok i tried it like this: ```@override initState(){ super.initState(); interval = widget.plant.interval; parsedDate = DateTime.parse(date); date = DateFormat.MMMMd().format(parsedDate); var dateNow = DateTime.now(); var dif = parsedDate.difference(dateNow).inDays; var difInPercent = double.parse((dif / interval).toStringAsFixed(2)); waterPercentage = difInPercent*100; } ``` But this doesn't seem to work. it doesnt initialize anything. – Subby Jun 23 '20 at 16:17
  • Or did I understand your answer wrong ? – Subby Jun 23 '20 at 16:20
  • It should work... (I guess). Did you try checking if `initState` is getting called? – Shardul Nalegave Jun 23 '20 at 16:35
  • Just to clear possible confusion. Here is the Updated Class https://pastebin.com/WPTiDgJD and yes the initState is getting called. – Subby Jun 23 '20 at 16:38
  • I checked the link you have provided but it gives a 404 error. – Shardul Nalegave Jun 23 '20 at 16:41
  • Sry try that again now. My fault there. – Subby Jun 23 '20 at 16:42

1 Answers1

0

UPDATE: Got it working now. @Shardul Nalegave's answer was right. i just used the wrong variables while initializing.

  initState(){
    super.initState();


    print("Super initState Call");
    
    interval = widget.plant.interval;
    parsedDate = DateTime.parse(widget.plant.date);
    date = DateFormat.MMMMd().format(parsedDate);

    var dateNow = DateTime.now();
    var dif = parsedDate.difference(dateNow).inDays;
    var difInPercent = double.parse((dif / interval).toStringAsFixed(2));
    waterPercentage = difInPercent*100;
    //print("WaterPercentage: $waterPercentage");
    //print("Date: $date");
    //print("Parsed Date: $parsedDate");
    //print("Interval: $interval");
  }
Subby
  • 1
  • 2