Dani
Dani

Reputation: 4177

Flutter: The argument type 'List<dynamic>' can't be assigned to the parameter type 'List<Widget>'

Getting this error with this:

return ListView(
  children: snapshot.data!.docs.map((index, DocumentSnapshot document) {
...
.toList()
  }

Without index, everything works fine

How would be possible to get an index here? Should I use a different loop to get it?

Upvotes: 1

Views: 243

Answers (3)

mohit48
mohit48

Reputation: 812

the children parameter of listView accepts a List as parameter your snapshot.data!.docs.map((index, DocumentSnapshot document) returns a List<dynamic> which is obvious because you haven't created widget list yet.

your method should be like this:

return ListView(
  children: snapshot.data!.map((value) => AnyWidget(value)).toList(),
);

Edit

I got your point. Assuming index is the index of list.

return ListView(
    children: snapshot.data!.map((document) {
      var index = snapshot.data!.indexOf(document);
      return Text('abc');
    }).toList()
);

better you convert snapshot.data into a List<yourModel> and use that instead of directly using it here.

Upvotes: 1

Dani
Dani

Reputation: 4177

I used ListView.builder:

return ListView.builder(
                itemCount: snapshot.data!.docs.length,
                itemBuilder: (context, index) {

Here the logic changes a bit and since I don't iterate over the list I need to assign it to a new var every time:

Map<String, dynamic> set = snapshot.data!.docs[index].data()! as Map<String, dynamic>;

Upvotes: 0

Quang Huy
Quang Huy

Reputation: 86

i think it should be return <Widget>

Ex:

return ListView(
  children: data.map((dynamic value) {
    // Handle data
    ...

    // Return <Widget>
    return Text('$value');
  }).toList(),
);

Upvotes: 0

Related Questions