Home > Blockchain >  Where do I use await to return a Future dart / flutter
Where do I use await to return a Future dart / flutter

Time:05-17

I am calling a future and I am returning a _InternalLinkedHashMap but I am getting the error type

'Future<dynamic>' is not a subtype of type 'Map<DateTime, List<Event>>'

I'm confused as I am awaiting the future and I get the Map which looks right when I print it out

// Gets the Future
FutureCalendarEvents datasource = const FutureCalendarEvents(); //Future Call

I then put into a function and await it

  Future<Map<DateTime, String>> myDates() async { //is My Dates a 'Future<dynamic>' ???

  final List<CalendarDetails> myDatesFuture = await  datasource.getCalendarFuture();    


   try {

      final Map<DateTime, String> _kEventSource = { 

      for (var item in myDatesFuture) 
      DateTime.parse(item.calendarTableTime) : item.calendarEventName 

      };

     print(_kEventSource.runtimeType); //_InternalLinkedHashMap<DateTime, String>

     return _kEventSource;

  } catch (e) {
    print("ERROR MESSAGE → → → "   e.toString() );
  }

}

This is the error I'm getting

The following _TypeError was thrown building LayoutBuilder:
type 'Future<dynamic>' is not a subtype of type 'Map<DateTime, List<Event>>'

Where the Map is returned to

final awaitedMyDate = await myDates(); // Must be in a function??

// Takes my events
final kEvents = LinkedHashMap<DateTime, String>(
  equals: isSameDay,
  hashCode: getHashCode,
)..addAll(myDates());

Then this is where kEvents is passed to

List<Event> _getEventsForDay(DateTime day)  {
    // Implementation example
    return  kEvents[day] ?? [];
  }

CodePudding user response:

you should change myDates to future type then wait for it :

Future <Map<DateTime, String>> myDates() async {

then

final awaitedMyDate = await myDates();

final kEvents = LinkedHashMap<DateTime, List<Event>>(
  equals: isSameDay,
  hashCode: getHashCode,
)..addAll(awaitedMyDate);

CodePudding user response:

myDates() async { //is My Dates a 'Future<dynamic>' ???

Yes, it is. You never gave it a type, so the compiler did the best it could. It's async, so it must return a future, but you never specified what type, so it's a Future<dynamic>.

If you want it to return a Future<Map<DateTime, List<Event>>> then you have to do so:

Future<Map<DateTime, List<Event>>> myDates() async {
  • Related