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;
}