Milly Alfaro
Milly Alfaro

Reputation: 309

How do I stop my code from converting firestore int data to a string in flutter

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:

enter image description here

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.

enter image description here

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?

Upvotes: 0

Views: 79

Answers (1)

Amit Kumar
Amit Kumar

Reputation: 307

In firestore data is stored as string you can't store data as int in firestore . If you want int value then you can convert it manually in code using

  int.parse('string')

Upvotes: 1

Related Questions