Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...