import 'package:intl/intl.dart';
late String day0;
late String dateMonth0;
late String day1;
late String dateMonth1;
late String day2;
late String dateMonth2;
late String day3;
late String dateMonth3;
late String day4;
late String dateMonth4;
late String day5;
late String dateMonth5;
void findDateTime(DateTime time) {
day0 = DateFormat('EEEEE', 'en_US').format(time);
dateMonth0 = DateFormat('d MMMM', 'en_US').format(time);
DateTime dateTime1 = DateTime(time.year, time.month, time.day 1);
day1 = DateFormat('EEEEE', 'en_US').format(dateTime1);
dateMonth1 = DateFormat('d MMMM', 'en_US').format(dateTime1);
DateTime dateTime2 = DateTime(time.year, time.month, time.day 2);
day2 = DateFormat('EEEEE', 'en_US').format(dateTime2);
dateMonth2 = DateFormat('d MMMM', 'en_US').format(dateTime2);
DateTime dateTime3 = DateTime(time.year, time.month, time.day 3);
day3 = DateFormat('EEEEE', 'en_US').format(dateTime3);
dateMonth3 = DateFormat('d MMMM', 'en_US').format(dateTime3);
DateTime dateTime4 = DateTime(time.year, time.month, time.day 4);
day4 = DateFormat('EEEEE', 'en_US').format(dateTime4);
dateMonth4 = DateFormat('d MMMM', 'en_US').format(dateTime4);
DateTime dateTime5 = DateTime(time.year, time.month, time.day 5);
day5 = DateFormat('EEEEE', 'en_US').format(dateTime5);
dateMonth5 = DateFormat('d MMMM', 'en_US').format(dateTime5);
}
above code is to fetch day date and month of upcoming 6 days using current date
i have created pickup and delivery page in which i want to show these dates.I have called the above function find date time in the init state of PickupScreen and the code is attached below but i am still getting late initialisation error on all the variables declared late any way i could solve this?
class PickupScreen extends ConsumerStatefulWidget {
const PickupScreen({Key? key}) : super(key: key);
@override
ConsumerState<ConsumerStatefulWidget> createState() => _PickupScreenState();
}
class _PickupScreenState extends ConsumerState<PickupScreen> {
@override
void initState() {
super.initState();
time();
}
void time() async {
DateTime time = await Time().getTime();
findDateTime(time);
}
CodePudding user response:
Check this Your concept added to a loop and it will help to get the values with the help of index. you can call the static function from your code and await for it to add the results to the list
CodePudding user response:
void time() async {
This function is asyncronous. That means it will run and return a Future
of some kind to notify you when it is done. Since yours does not return a future (why is your linter not flagging this? Did you turn it off?) you are unable to determine when it is done.
So when you call it:
void initState() {
super.initState();
time();
}
It will return immediately. The point is that the Future it returns can be used with the await
keyword to make sure it is finished. But you skipped that by chosing to return void
.
So it will return immediately, but unlike non-async function, it will not actually be done. So your initState
completes and the build starts, but your variables declared as late
aren't actually assigned yet. That is why you get the exception.
There is no easy fix here. You have to understand how Futures and async/await patterns work and then find the best solution to your problem.
A good start might be What is a Future and how do I use it?