When I read data from firebase, it show my data. But it also show all user data. Not only my data. But I want only my data. Not other user - flutter

For Write Data. Here is the code. I call data from users account. But there are multiple user data. I only show my data. But it shows all user data.
getDriversList() async {
return await Firestore.instance.collection('users').getDocuments();
}
QuerySnapshot querySnapshot;
#override
void initState() {
super.initState();
getDriversList().then((results) {
setState(() {
querySnapshot = results;
});
});
}

You can use userId as documentID when create those users document.
Then callawait Firestore.instance.collection('users').document(your user Id).get(); to get users own data.

Related

Flutter Asynchronous Data Retrieval

My app has 3 main pages:
Listings Home
Social Home
Search Home
When the user logs in I create a local "User" object, in the constructor I fire off 3 async functions that are supposed to retrieve the required data for the above 3 pages:
class User {
ParseUser user_;
ParseObject user_info_;
Position? current_location_;
bool retrieve_related_data_;
List<Listing> listings_ = []; // data for Listings Home
List<Conversation> conversations = []; // data for Social Home
List<Search> serach_results_ = []; // data for Search home
User(this.user_, this.user_info_, this.current_location_, this.retrieve_related_data_) {
if(retrieve_related_data_) {
print("RETRIEVING USER DATA");
getListings();
getConversations();
getSearchResults();
}
}
// returns all listings connected to the logged in user
void getListings() async {
listings_ = (await back4app_listing.getListings(user_info_));
}
// returns all conversations connected to the logged in user
void getConversations() async {
conversations = (await back4app_conversation.getConversations(user_info_));
}
// returns all search results connected to the logged in user
void getSearchResults() async {
serach_results_ = (await back4app_search.getSearchResults(user_info_))
}
}
If the user for example navigates to the Listing Home page before the getListings() function has finished and put the data in the listing_ list, how do I await for the function to complete? Same scenario if they were to navigate to the other pages before the functions had returned. I want the pages states to update once these functions return from retrieving data.
I was thinking of creating bools for each async function and toggling them from false to true depending on the state the function is in, but I feel that there is a better way to implement this.

Getting all documents uids from firestore and add them to a list .flutter

I created a new collection named users on my firestore project. Im trying to get a list of all auto generated id's in that collection. So far I tried
late List<String> userID = [];
Future getID() async {
await FirebaseFirestore.instance.collection('users').get().then(
(snapshot) => snapshot.docs.forEach((document) {
userID.add(document.reference.id);
}),
);
}
But whenever I try to access the Strings of id's in the list , it returns an empty list
getID is a future method. It will take some to fetch data. After getting data you need to call setState to update the UI. You dont need to await and .then same time
try
Future getID() async {
FirebaseFirestore.instance.collection('users').get().then((snapshot) {
snapshot.docs.forEach((document) {
userID.add(document.reference.id);
});
setState(() {});
});
}
It would be great to use FutureBuilder for future method.

Does streambuilder from firebase rtdb will update list<User> user data?

currently I understadn with the method streamBuilder I can fetch updated data and add in the List<User> users.
But what if this user which is already added in the List<User> users has updated data, and then it could be double adding this user data in the List<User> users right?
Could you plz show me how to confirm whether for the new coming data List<User> users has already same userId, if yes, the new data / userId will replace this exisiting userId?
If the user is deleted from Firebase rtdb, the stream will be notified, and therefore remove this user from List<User> users?
here is example, my concern is since stream will always add data to the List users, but what if this user is removed from database or disconnect, how to remove this user from this list?
_streamSubscription = availableUserStream.onValue.listen((snap) {
if (snap.snapshot.exists && snap.snapshot.value != null) {
DataSnapshot snapshotData = snap.snapshot;
for (var userSnapshot in snapshotData.children) {
final data = Map<String, dynamic>.from(userSnapshot.value as Map);
List<User> users = [];
User newUser = User.fromJson(data);
users.add(newUser);
firebaseController.setUsers(users: users);
}
}
});
So I thought to do a double confirm here if this user is still exisitng in the database:
User getRandomSenderUser({User asReceiverUser}) {
if (availableSenderUsersList.isNotEmpty) {
final random = Random();
var i = random.nextInt(availableSenderUsersList.length);
User randomUser = availableSenderUsersList[i];
bool thisRandomUserIsAvailable; //TODO
I don't know how to do this check, e.g. if this randomerUser is unavailable, so I need to get next randomUser, so it should be a loop? But it will slow down the response speed.
updateSenderUserAvailableStatus(asReceiverUser:asReceiverUser,connectionUser: randomUser);
return randomUser;
} else {
return null;
}
}
thank you!
Update:
Here is the example code, so now I understand stream will pass user data to List<User> users, but in my way there will always be user who is added in this list before, but was already removed from database, my plan is using while loop for double confirming to remove unavailable user when getting the randomUser, but it sounds not smart and still waste time I guess....
#override
void initState() {
_listenAvailableUsers();
}
_listenAvailableUsers() {
var availableUserStream =
FirebaseDatabase.instance.ref().child('/waitingList');
_streamSubscription = availableUserStream.onValue.listen((snap) {
if (snap.snapshot.exists && snap.snapshot.value != null) {
DataSnapshot snapshotData = snap.snapshot;
for (var userSnapshot in snapshotData.children) {
final data = Map<String, dynamic>.from(userSnapshot.value as Map);
List<User> users = [];
User newUser = User.fromJson(data);
users.add(newUser);
firebaseController.setUsers(users: users);
}
}
});
}
Here is the method I though to confirm if the randomUser is still existing in the database:
Future<User> getRandomSenderUser({User asReceiverUser}) async {
if (availableSenderUsersList.isNotEmpty) {
User randomUser;
while (true) {
final random = Random();
var i = random.nextInt(availableSenderUsersList.length);
randomUser = availableSenderUsersList[i];
DatabaseEvent event = await databaseReference
.child('/waitingList/${randomUser.userId}')
.once();
print('randomUser is ${randomUser.toString()}');
if (event.snapshot.value != null) {
break;
}
}
await updateSenderUserAvailableStatus(
asReceiverUser: asReceiverUser, connectionUser: randomUser);
print('connectionUserId is $connectionUserId');
return randomUser;
} else {
return null;
}
}
Since you're listening to the onValue of a path in the database, the DataSnapshot you get will contain the entire data at that path. When there was only a small change in the data, the server will only send that update to the client, but the SDK will then merge that with the existing data and still fire an event with a snapshot of all the data at the path.
Since you're starting with an empty list (List<User> users = [];) each time you get an event from the stream, that means you're rebuilding the entire lit of users each time, which seems correct to me.

How to fetch data from Firestore and display them in Flutter?

I'm a beginner in Flutter and firestore. I have a collection in firestore with following order:
event->'a user specific id'->post->'a post id->'post details'. you can see hereFirestore1 and hereFirestore2
When I try to fetch the 'postdetails', only thing I get is 'Instance of 'DocumentSnapshot',see hereResponse
What i tried:
getEvents() async {
setState(() {
_isLoading = true;
});
DocumentSnapshot snapshot = await eventref
.doc(uid)
.collection('post')
//.orderBy('Date', descending: true)
.doc()
.get();
print('Snapshot : ${snapshot}');
return snapshot;
// setState(() {
// _isLoading = false;
// print(event);
// });
}
I have also made a model for events. See hereEvent Model
I want to fetch data and display them as card.
Any help?
Thanks in Advance
print('Snapshot : ${snapshot.data()["Date"]}');
Using data() you can access the fields value.
snapshot.get("field_name")
You need to ask flutter to retrieve the fields inside the document snapshot.
Will need a more detailed order of your firestore collection and document to provide the exact code. You may follow the sample below:
On firestore:
-Collection: "users"
---Document: "userId"
-------Field: "username"
-------Field: "birthdate"
Code:
DocumentSnapshot doc =await _firestore.collection("users").doc(userId).get();
print(doc.get('username'));
print(doc.get('birthdate'));

Flutter: Firebase user's name doesn't get updated when user logs in for the first time

I am trying to fetch a user's ID when a person is logged in with their firebase account. When I try to login a new user for the first time, I can fetch their id however the user's details only reflect after the person navigates from one page to another using the navigation bar. Is there a way I can get rid of that? I want the user's display name to reflect as soon as they create a new account.
This is my current code that I call in my initState()
getCurrentUser() async {
try {
final user = await _auth.currentUser();
if (user != null) {
setState(() {
loggedInUser = user;
id = user.uid;
username = user.displayName;
});
}
} catch (e) {
print(e);
}
}