The element type 'Iterable<Widget>' can't be assigned to the list type 'Widget'

Viewed 2902

I am making an app using sqlite, and when I try to put the data into widget, I am having an error.. I am following a tutorial and it's giving me this error: The element type 'Iterable' can't be assigned to the list type 'Widget'.

 FutureBuilder<List<Reminder>>(
          future: _reminders,
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              _currentReminders = snapshot.data!;
              return ListView(children: [
                snapshot.data!.map<Widget>((reminder) {
                  return ReminderCard(
                      name: reminder.name, details: reminder.details);
                }).toList(),
  • new error message: the element type 'List' can't be assigned to the list type 'Widget'.dartlist_element_type_not_assignable

The element type 'List' can't be assigned to the list type 'Widget'.dartlist_element_type_not_assignable Iterable map(T Function(Reminder) f) dart:core Returns a new lazy [Iterable] with elements that are created by calling f on each element of this Iterable in iteration order. This method returns a view of the mapped elements. As long as the returned [Iterable] is not iterated over, the supplied function [f] will not be invoked. The transformed elements will not be cached. Iterating multiple times over the returned [Iterable] will invoke the supplied function [f] multiple times on the same element. Methods on the returned iterable are allowed to omit calling f on any element where the result isn't needed. For example, [elementAt] may call f only once. Reminder is the class for DB elements.

 Reminder(
      {this.id = 0,
      required this.name,
      required this.details,
      required this.save});

  factory Reminder.fromMap(Map<String, dynamic> json) => Reminder(
      id: json["id"],
      name: json["name"],
      details: json["details"],
      save: json["false"]);

  Map<String, dynamic> toMap() => {
        "id": id,
        "name": name,
        "details": details,
        "save": save,
      };
}

can you help me fix this?

1 Answers
  1. snapshot.data!.map<widget> - I do not expect that widget is a valid type, it should be Widget.

  2. If you want to use the .map function inside ListView children, you should convert map result - iterable - to list and use the spread operator to spread this list inside children:

    ListView(children: [
      ...snapshot.data!.map<widget>((reminder) {
        return ReminderCard(
          name: reminder.name, details: reminder.details);
      }).toList(),
    
Related