IconButton(
onPressed: () async {
DateTime? x = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime.now(),
lastDate: DateTime(2040));
if (x == null) return;
setState(() {
final DateFormat formatter = DateFormat('yyyy-MM-dd');
String formattedDate = formatter.format(x);
print(formattedDate);
print(formattedDate.runtimeType);
});
},
icon: const Icon(UniconsLine.clock)),
Text(formattedDate ?? "EMPTY"),
I am seeing always empty my formattedDate variable below on the build method why doesnt work this code
3
Answers
Could you try to lift up the formattedDate? I think the Problem is that your variable is out of scope.
The problem is in the scope of your variable formattedDate. It only exists inside setState because it was declared there.
Declare it at the beginning of the class.
You have redefined
formattedDate
insidesetState()
as a local variable. The fieldformattedDate
you are using inText(formattedDate ?? "EMPTY")
is a totally different variable. It remains null, as you are not changing it at all. Just remove theString
beforeformattedDate
and it should be fine.