User? user = FirebaseAuth.instance.currentUser;
late Stream<QuerySnapshot<Map<String, dynamic>>> stream = FirebaseFirestore
.instance
.collection('contents')
.doc(user!.uid)
.collection('content')
.snapshots();
This user!.uid is uid of the current user. I need to get uid of all the users.
Related
I want to build a contactScreen for my flutter app. Therefor I have to download an array from Firebase. I am just able to download directly into a listView in flutter and get stuck while coding. Heres my code:
var currentUser = FirebaseAuth.instance.currentUser!.uid;
var contacts;
getUserData() async {
var userData = await FirebaseFirestore.instance
.collection('users')
.where('uid', isEqualTo: currentUser)
.get();
contacts = userData['contacs']; //heres the error
}
At first I want to initialize the currentUser's UID and then get the currentUser's contacts array from firebase. Therefor I build the getUserData() method to download the User and then initialize his contacts array.
The last step doesn't work in Flutter, I can't access the contacts array. Is the way I want to get the data correct?
You're at the very least missing an await before the get() call:
var userData = await FirebaseFirestore.instance
.collection('users')
.where('uid', isEqualTo: FirebaseAuth.instance.currentUser!.uid)
.get();
Without await your userData is of type Future<QuerySnapshot<Map<String, dynamic>>> as you get in the error message. By using await, its type will become QuerySnapshot<Map<String, dynamic>>.
you need to call await or use FutureBuilder
like this
FutureBuilder(
future: FirebaseFirestore.instance
.collection('users')
.where('uid', isEqualTo: FirebaseAuth.instance.currentUser!.uid)
.get(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Column(
children: [Text(snapshot.data['name'])], //error here
);
}
return Loading();
},
)
I have tasks subcollection for every user, How can I delete tasks ,
CollectionReference users = FirebaseFirestore.instance.collection('users');
final FirebaseAuth _auth = FirebaseAuth.instance;
Future<void> deleteTask() {
return users
.doc(user.uid)
.collection('Tasks').delete() ?? ??
.then((value) => print("Tasks Deleted"))
.catchError((error) => print("Failed to delete task: $error"));
}
To delete all documents in collection or subcollection with Cloud Firestore, you can use the delete method iterating on a DocumentReference:
CollectionReference users = FirebaseFirestore.instance.collection('users');
Future<void> deleteAllTaskDocs() {
return users
.doc(user.uid)
.collection('Tasks')
.get()
.then((QuerySnapshot querySnapshot) {
querySnapshot.docs.forEach((doc) {
doc.reference.delete();
});
});
}
Keep in mind that:
There is no operation that atomically deletes a collection.
Deleting a document does not delete the documents in its subcollections.
If your documents have dynamic subcollections, it can be hard to know what data to delete for a given path.
Deleting a collection of more than 500 documents requires multiple batched write operations or hundreds of single deletes.
How to read subcollection from flutter firestore. I am using cloud_firestore. I am successfully adding data into firestore but couldn't retrieve it(tried and failed).
I want to retrieve subCollection called product Firestore collection
I tried this I don't have the document ID Because it generated automatically from firestore :
Stream<QuerySnapshot> loadorderdetails(String uid) {
return FirebaseFirestore.instance
.collection('Users/$userid/Orders')
.doc()
.collection("Products")
.where('uid', isEqualTo: uid)
.snapshots();
}
You should try this
FirebaseFirestore.instance.collection("Users/$userid/Orders ").get().then((querySnapshot) {
querySnapshot.docs.forEach((result) {
FirebaseFirestore.instance
.collection("Users/$userid/Orders")
.doc(result.id)
.get()
.then((querySnapshot) {
//print data
});
});
});
});
Using the below code in my flutter app, It can display all the delivery details in my correction;
Stream<QuerySnapshot> getUsersShippingStreamSnapshots(
BuildContext context) async* {
final uid = await Provider.of(context).auth.getCurrentUID();
yield* Firestore.instance
.collection('userData')
.document(uid)
.collection('newShipping')
.orderBy('placedDate', descending: true)
.snapshots();
}
Now, I want to display only the delivery details which has delivered status, but I'm getting an error using this code;
Stream<QuerySnapshot> getUsersShippingStreamSnapshots(
BuildContext context) async* {
final uid = await Provider.of(context).auth.getCurrentUID();
yield* Firestore.instance
.collection('userData')
.document(uid)
.collection('newShipping')
.where('status', isEqualTo: 'pending')
.orderBy('placedDate', descending: true)
.snapshots();
Help me fix this code.
i want to get the user creation timestamp, is there any way to do this?
im using google sign in auth.
Any help is appreciated!
Edit: i cant find the user creation timestamp when printing the whole user
Edit 2: Code I am using for authentication:
GoogleSignInAccount currentUser;
final GoogleSignIn googleSignIn = GoogleSignIn();
final FirebaseAuth auth = FirebaseAuth.instance;
Future<FirebaseUser> signIn() async {
GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
GoogleSignInAuthentication gSA = await googleSignInAccount.authentication;
FirebaseUser user = await auth.signInWithGoogle(
idToken: gSA.idToken, accessToken: gSA.accessToken);
print('Signed In as ${user.displayName}');
return user;
}
You can get the creation timestamp in epoch format with:
user.metadata.creationTime
See:
metadata