'How can i catch null as an exception and do some stuff on catching the null-exception in Flutter
I have a piece of code where I check firebase Cloud Firestore for a user using their phone number.
void addUsertoChama(String phone) {
FirebaseFirestore firestore = FirebaseFirestore.instance;
firestore
.collection('Users')
.where('Phone', isEqualTo: phone)
.get()
.then((QuerySnapshot querySnapshot) {
final s = querySnapshot.docs.single;
UserChama userchama =
UserChama(id: s['Id'], phone: s['Phone'], name: s['Name']);
print(userchama.name);
Provider.of<UserChamaNotifier>(context, listen: false)
.addUserChama(userchama);
});
} }
The query snapshot returned has a single document, the user queried. I am assuming that is the user is not found then the query snapshot will return a null value. Is there a way i can catch null as an error, inform the user that the account/user does not exist and prompt them to invite the user to download my app.
Solution 1:[1]
You must go with the NULL aware operator available in the Dart.
Check below link.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | SYED TOUSIF |