Vit Amin
Vit Amin

Reputation: 683

Flutter + Firestore error: The method '[]' can't be unconditionally invoked because the receiver can be 'null'

The following code throws this error "The method '[]' can't be unconditionally invoked because the receiver can be 'null'"

if (snapshot.hasData == true) {
          return ListView(
            children: snapshot.data!.docs.map((DocumentSnapshot document) {
                return ListTile(
                  title:  Text(document.data()['title']),
                );
            }).toList(),
          );
        }

Is it related to null-safety? how to fix it?

Upvotes: 2

Views: 827

Answers (2)

Vit Amin
Vit Amin

Reputation: 683

This problem is related to a Flutter update.

In the newest Flutter update, there is no need in adding the .data().

Removing the.data() from the code in the description solves the issue.

Upvotes: 8

sungkd123
sungkd123

Reputation: 403

Try doc.get('title')

instead of document.data()['title']

Upvotes: 3

Related Questions