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

Time:01-29

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?

CodePudding user response:

i think it should be return <Widget>

Ex:

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

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

CodePudding user response:

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

CodePudding user response:

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>;
  •  Tags:  
  • Related