Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

type 'List<dynamic>' is not a subtype of type 'List<Widget>'

I have a snippet of code which I copied from Firestore example:

Widget _buildBody(BuildContext context) {
    return new StreamBuilder(
      stream: _getEventStream(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) return new Text('Loading...');
        return new ListView(
          children: snapshot.data.documents.map((document) {
            return new ListTile(
              title: new Text(document['name']),
              subtitle: new Text("Class"),
            );
          }).toList(),
        );
      },
    );
  }

But I get this error

type 'List<dynamic>' is not a subtype of type 'List<Widget>'

What goes wrong here?

like image 370
Arash Avatar asked Apr 01 '18 22:04

Arash


People also ask

Is not a subtype of type list dynamic >? In flutter?

To solve type 'List' is not a subtype of type 'List' Error you just need to assign a type to the map method. Here I am giving type to map<Widget> Or just converting Map to Widget will also solve your error. To solve type 'List' is not a subtype of type 'List' Error you just need to assign a type to the map method.

How do you convert a list list to dynamic string in flutter?

In dart and flutter, this example converts a list of dynamic types to a list of Strings. map() is used to iterate over a list of dynamic strings. To convert each element in the map to a String, toString() is used. Finally, use the toList() method to return a list.


1 Answers

The problem here is that type inference fails in an unexpected way. The solution is to provide a type argument to the map method.

snapshot.data.documents.map<Widget>((document) {
  return new ListTile(
    title: new Text(document['name']),
    subtitle: new Text("Class"),
  );
}).toList()

The more complicated answer is that while the type of children is List<Widget>, that information doesn't flow back towards the map invocation. This might be because map is followed by toList and because there is no way to type annotate the return of a closure.

like image 81
Jonah Williams Avatar answered Oct 18 '22 09:10

Jonah Williams