chubby marschmallow
chubby marschmallow

Reputation: 59

The argument type 'List<Null>?' can't be assigned to the parameter type 'List<Widget>'

I want to create a chat for my application. I need to display the name field of all users I have in my users collection in firebase using flutter. The following image is similar to what I am trying to do.

[1]

Here is my code :

          stream: FirebaseFirestore.instance.collection('users').snapshots(),
          builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
            if (!snapshot.hasData) {
              return Text("You have no contact.");
            }
            return ListView (
              children: snapshot.data?.docs.map(
                (doc){ 
                  new Text(doc["fullName"]);
                  }
              ).toList(), // all the children is making an error
            );
          }
        )

And here is the problem I have : The argument type 'List<Null>?' can't be assigned to the parameter type 'List<Widget>'..

I guess the problem comes from my snapshot which isn't wrapped in a Widget, but the code I took inspiration from did the same thing and doesn't seem to have any problem. Their code : return ListView( children: snapshot.data.documents.map((document){}).toList(),

Has anyone any idea ? Thank you for reading me

Upvotes: 1

Views: 3202

Answers (2)

Hichem Benali
Hichem Benali

Reputation: 411

one of the solutions is to add type to the map method.

ListView (
          children: snapshot.data?.docs.map<Widget>(
            (doc){ 
              new Text(doc["fullName"]);
              }
          ).toList(), // all the children is making an error
        );

Upvotes: 0

Sajad Abdollahi
Sajad Abdollahi

Reputation: 1741

You have declared your function with block body but you are not using return command to return the widget!

Fix it by returning the Text widget:

            return ListView (
              children: snapshot.data?.docs.map(
                (doc){ 
                  return new Text(doc["fullName"]);
                  }
              ).toList(), // all the children is making an error
            );

You can also replace the block body with an arrow fucntion:

            return ListView (
              children: snapshot.data?.docs.map(
                (doc) => 
                   new Text(doc["fullName"])
              ).toList(), // all the children is making an error
            );

FIX: if you are facing with error

The argument type 'List<Text>?' can't be assigned to the parameter type 'List<Widget>'

then cast the list to a List<Widget>:

snapshot.data?.docs.map(
                (doc) => 
                   new Text(doc["fullName"])
              ).toList() as List<Widget>

Upvotes: 2

Related Questions