JohnnyQ
JohnnyQ

Reputation: 543

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

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) : 
        [Event(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

 kSources ()  async {
  final Map<DateTime, List<Event>> awaitedMyDate = await myDates();
  return awaitedMyDate;
}

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

Upvotes: 0

Views: 216

Answers (2)

manhtuan21
manhtuan21

Reputation: 3430

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

Upvotes: 1

nvoigt
nvoigt

Reputation: 77294

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 {

Upvotes: 0

Related Questions