Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

firebase - Store array from cloud firestore into variable

I have a collection called users. Each document has the ids of posts they have saved in an array. Is it possible to put this array into a variable and have it in a Text() widget? Essentially the text widget output would look like this on screen oMqSYa97CteaDr19lZ5Ga6WO4jn8oPJREOcJWLI2.

I have searched for this solution, but haven't found any luck.

I've tried a few things, but usually end up with Instance of 'Future DocomentSnapshot'

image showing array

question from:https://stackoverflow.com/questions/65547114/store-array-from-cloud-firestore-into-variable

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

1 Answer

0 votes
by (71.8m points)

you should share your code for us to learn what you did and why do you face with that oMqSYa97CteaDr19lZ5Ga6WO4jn8oPJREOcJWLI2. However, firstly, you should create a model like that

class UserData {

  final String uid;
  final String name;
  final String sugars;
  final int strength;

  UserData({ this.uid, this.sugars, this.strength, this.name });

}

and an instance;

  final CollectionReference UsersCollection =
      FirebaseFirestore.instance.collection('users');

and you can use it like that ;

  UserData _userDataFromSnapshot(DocumentSnapshot snapshot) {
    return UserData(
      uid: uid,
      name: snapshot.data()['name'],
      sugars: snapshot.data()['sugars'],
      strength: snapshot.data()['strength'],
    );
  }

  Stream<UserData> get userData {
    return UsersCollection.doc(uid).snapshots().map(_userDataFromSnapshot);
  }

finally;

UserData userData = snapshot.data.sugars;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...