I have two collections set up with one for individual users can one for groups that users can join. In the individual collection users have an int data called plastics, but when I try bringing that int data to be shown in groups, it converts into a string in firestore. This is bad as I need this data to stay an int as users have the ability to add to it, thus when it becomes a string users can no longer update that value in the group's collection only for their individual collection.
Here a photo of an individual user's collection:
As you can see the int data called plastics is saved as an int.
Now, here's a photo of the group's collection that is using the same information yet the plastics is saved as a string not an int.
Here the code that I think may be causing the int to turn into a string.
@override
Widget build(BuildContext context) {
final firestore = FirebaseFirestore.instance;
FirebaseAuth auth = FirebaseAuth.instance;
Future<String> getPlasticNum() async {
final CollectionReference users = firestore.collection('UserNames');
final String uid = auth.currentUser.uid;
final result = await users.doc(uid).get();
return result.data()['plastics'].toString();
}
This future was meant for only individual users so that their int data is converted into a string so that I can display it as text.
When I try getting their int data again for groups:
@override
Widget build(BuildContext context) {
final CollectionReference users = firestore.collection('UserNames');
final String uid = auth.currentUser.uid;
return FutureBuilder(
future: users.doc(uid).get(),
builder: (context, snapshot) {
if (snapshot.hasData) {
final result = snapshot.data;
final groupId = result.data()['groupId'];
return FutureBuilder<QuerySnapshot>(
// <2> Pass `Future<QuerySnapshot>` to future
future: FirebaseFirestore.instance
.collection('Groups')
.doc(groupId)
.collection('Members')
.orderBy('plastics', descending: true)
.get(), //Once the async problem is solved i will be able to save the groupId as. variable to be used in my doc path to access this collection. How do I do this?
builder: (context, snapshot) {
if (snapshot.hasData) {
// <3> Retrieve `List<DocumentSnapshot>` from snapshot
final List<DocumentSnapshot> documents = snapshot.data.docs;
return ListView(
children: documents
.map((doc) => Card(
child: ListTile(
title: Text(doc['displayName']),
subtitle: Text(doc['plastics']),
),
))
.toList());
} else if (snapshot.hasError) {
return Text('Its Error!');
}
});
}
});
}
This shows the int data in firestore as a string.
I don't understand why even though I am grabbing the same data from another collection, it is becoming a string as in the individual collection it is saved an int?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…