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

Viewed 700

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?

2 Answers

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.

Try doc.get('title')

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

Related