JohnnyQ
JohnnyQ

Reputation: 543

the 'for' loop must implement 'Iterable<dynamic>' on a Future function

I'm trying to return a map but the way I'm trying to iterate over the list but the way I am integrating over it is causing a problem.

I'm getting this error Error: The type 'Future<List> Function()' used in the 'for' loop must implement 'Iterable'.

///Model Event  
class Event {
  final int id;
  final bool free;
  final String epoch;
  final String address;
  final bool all_day;
  final String end_date;
  final String description;
  final String start_date;

  Event(
    this.id,
    this.free,
    this.epoch,
    this.address,
    this.all_day,
    this.end_date,
    this.description,
    this.start_date,
  );

  @override
  String toString() =>
      "$id, $free, $epoch, $address, $all_day, $end_day, $description, $start_date";
}

This returns a List<event>

///Future to get information from the API
Future<List<Event>> eventList() async {
  var response = await http.get(Uri.parse(Constants.EVENTS_API));
  if (response.statusCode == 200) {
    var jsonData = json.decode(response.body);
    //print(jsonData);

    List<Event> allEvents = [];

    for (var map in jsonData) {
      Event events = Event(
        map["id"],
        map["free"],
        map["epoch"],
        map["address"],
        map["all_day"],
        map["end_date"],
        map['description'],
        map["start_date"],
      );
      allEvents.add(events);
    }
    return allEvents;
  } else {
    throw Exception('Failed to load');
  }
}

I then try to add it to a new Map

mydates() {
  var eventDayMap = <DateTime, List<Event>>{};

  for (var event in eventList()) {
    (eventDayMap[DateTime.parse(event.start_date)] ??= []).add(event);
  }
  return eventDayMap;
}

the mydates function is called from here

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

Here is the function receiving the kEvents

List<Event> _getEventsForDay(DateTime day) {
  

    return kEvents[day] ?? [];
  }

What have I got to do to implement a 'Iterable<dynamic>', iterables like values or keys won't work.

Thanks

Upvotes: 0

Views: 4478

Answers (1)

Roaa
Roaa

Reputation: 1440

Your eventList() function returns a Future that returns a list, so you can't use it directly in your for loop. Try changing your mydates() like so:

Future<Map<DateTime, List<Event>>> mydates() async {
  var eventDayMap = <DateTime, List<Event>>{};

  List<Event> _allEvents = await eventList();
  for (var event in _allEvents) {
    (eventDayMap[DateTime.parse(event.start_date)] ??= []).add(event);
  }
  return eventDayMap;
}

OR:

Future<Map<DateTime, List<Event>>> mydates() async {
  var eventDayMap = <DateTime, List<Event>>{};

  for (var event in await eventList()) {
    (eventDayMap[DateTime.parse(event.start_date)] ??= []).add(event);
  }
  return eventDayMap;
}

UPDATE: Where you are calling your mydates() function, you need to await it

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

OR

late LinkedHashMap<DateTime, List<Event>> kEvents;
mydates().then((_myDates) {
  if(_myDates != null) {
    kEvents = LinkedHashMap<DateTime, List<Event>>(
      equals: isSameDay,
      hashCode: getHashCode,
    )..addAll(_myDates);
  }
});

I also added return types to the functions above as suggested by the comment


Upvotes: 2

Related Questions