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
)
);
}
);
} );
CodePudding user response:
Change this :
chatdocs[index]['userId'] == futureSnapshot.data!.uid, //this is the error
to this :
chatdocs[index]['userId'] = futureSnapshot.data!.uid,
CodePudding user response:
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 the uid
as a map key to check with chatdocs[index]['userId']
Like this:
import 'dart:convert';
final Map<String, dynamic> _valueMap = json.decode(futureSnapshot.data!);
chatdocs[index]['userId'] == _valueMap['uid'];
CodePudding user response:
Since you don't declare the type of the Future
in your FutureBuilder
, it resolves to an Object
. And an Object
doesn't have a uid
property, which is why you get the error.
To solve this declare the type of your FutureBuilder
, which in your case would be:
return FutureBuilder<User>(
Note that I have no idea why you're using a FutureBuilder
here to begin with. The FirebaseAuth.instance.currentUser
is a synchronous value, so you don't need a FutureBuilder
to access it. Removing the FutureBuilder
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 a StreamBuilder
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 your StreamBuilder
with a User
type, just like we did above for the FutureBuilder
.
CodePudding user response:
Try the following code:
StreamBuilder<QuerySnapshot>(
stream: firestore.collection('chats').orderBy('timeStamp', descending: true).snapshots(),
builder: (ctx, chatSnapshot) {
if (chatSnapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(),
);
}
final List<QueryDocumentSnapshot<Object?>> chatdocs = chatSnapshot.data!.docs;
final String uid = FirebaseAuth.instance.currentUser?.uid ?? '';
return ListView.builder(
reverse: true,
itemCount: chatdocs.length,
itemBuilder: (ctx, index) {
final Map<String, dynamic> chat = (chatdocs[index]).data() as Map<String, dynamic>;
return messageBubble(
chat['text'],
chat['userId'] == uid,
);
},
);
},
),