i'm working with firebase and flutter and i got this two error : (1) => Error: Property 'uid' cannot be accessed on 'User?' because it is potentially null (2) => Error: Property 'docs' cannot be accessed on 'QuerySnapshot<Object?>?' because it is potentially null
Widget build(BuildContext context) {
return Scaffold(
body: Container(
decoration: BoxDecoration(color: Colors.pink),
width: MediaQuery.of(context).size.width ,
height: MediaQuery.of(context).size.height,
child : Center(child: StreamBuilder(
stream : FirebaseFirestore.instance.collection('Users')
.doc(FirebaseAuth.instance.currentUser.uid) (1)
.collection("Coins")
.snapshots(),
builder: (BuildContext context ,
AsyncSnapshot<QuerySnapshot> snapshot ) {
if(!snapshot.hasData){
return Center(
child: CircularProgressIndicator(),
);
}
return ListView(
children: snapshot.data.docs.map((document) { (2)
return Container(
child : Row(
children : [
Text("Coin Name : "),
Text("Amount Owned : "),
] ,
),
);
}).toList(),
);
}
),
),
),
);
}
CodePudding user response:
To remove potential error you can apply a if condition before using the code that can give potential null error like
if(FirebaseAuth.instance.currentUser.uid!=null){....}
CodePudding user response:
This means the User can be null. To access a property on a nullable object, you need to use ?.
(return null if null) or !.
(throw an error if it's null).
In most cases you should use ?.
.
FirebaseAuth.instance.currentUser?.uid
...
children: snapshot.data?.docs.map((document) {