Skip to content

Commit 95ab31d

Browse files
authored
docs(firestore): handle non-existent document (#5853)
Calling `snapshot.data.data()` on a non-existing `DocumentSnapshot` leads to: > The method '[]' was called on null. I see this error pop up a lot on Stack Overflow, so this behavior is not obvious to developers. Explicitly including a `exists` check in this code sample should lead to move devs doing the right thing.
1 parent 8115cd5 commit 95ab31d

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

docs/firestore/usage.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ class GetUserName extends StatelessWidget {
103103
if (snapshot.hasError) {
104104
return Text("Something went wrong");
105105
}
106+
107+
if (snapshot.hasData && !snapshot.data.exists) {
108+
return Text("Document does not exist");
109+
}
106110
107111
if (snapshot.connectionState == ConnectionState.done) {
108112
Map<String, dynamic> data = snapshot.data.data();

0 commit comments

Comments
 (0)