i am trying to check if a user id is the same as the current user’s id by using data.uid but i keep getting this error:
The getter ‘uid’ isn’t defined for the type ‘Object’.
this is the code
Widget build(BuildContext context) {
return FutureBuilder(
future: Future.value(FirebaseAuth.instance.currentUser),
builder: (context, futureSnapshot){
if(futureSnapshot.connectionState == ConnectionState.waiting){
return Center(child: CircularProgressIndicator(),);
}
return StreamBuilder <QuerySnapshot>(
stream: firestore.collection('chats').orderBy('timeStamp', descending: true).snapshots(),
builder:(ctx, chatSnapshot){
if(chatSnapshot.connectionState == ConnectionState.waiting){
return Center(child: CircularProgressIndicator(),);
}
final chatdocs = chatSnapshot.data!.docs;
return ListView.builder(
reverse: true,
itemCount: chatdocs.length ,
itemBuilder: (ctx, index) => messageBubble(
chatdocs[index]['text'],
chatdocs[index]['userId'] == futureSnapshot.data!.uid, //this is the error
)
);
}
);
} );
3
Answers
You can try json decoding your variable into the object User you are looking for:
If the
futureSnapshot.data
is a user you’ll be able to use theuid
as a map key to check withchatdocs[index]['userId']
Like this:
Since you don’t declare the type of the
Future
in yourFutureBuilder
, it resolves to anObject
. And anObject
doesn’t have auid
property, which is why you get the error.To solve this declare the type of your
FutureBuilder
, which in your case would be:Note that I have no idea why you’re using a
FutureBuilder
here to begin with. TheFirebaseAuth.instance.currentUser
is a synchronous value, so you don’t need aFutureBuilder
to access it. Removing theFutureBuilder
would lead to the exact same result.If you’re trying to make your code respond to auth state changes, like that it takes a moment for Firebase to restore the user’s sign-in state when you start the app, you’ll want to actually listen to
authStateChanges
with aStreamBuilder
for that nowadays, as shown in the first code snippet in the documentation on getting the current user. Here too, you’ll want to declare yourStreamBuilder
with aUser
type, just like we did above for theFutureBuilder
.Try the following code: