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
215 views
in Technique[技术] by (71.8m points)

flutter - How can I get data from a Map on Firebase where the values are 'true'?

I have this Map likes there I use to save the information of people who liked a post. I save the field with the idof each user, and I would like to access this field but only the documents that are true and use the id to get the users profile. How can I access the Map and get the ids?

enter image description here

question from:https://stackoverflow.com/questions/65929296/how-can-i-get-data-from-a-map-on-firebase-where-the-values-are-true

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

1 Reply

0 votes
by (71.8m points)

As I understand, you want to access the list of user ids that are in your likes map. In that case, you can do this:

// Get the post
final DocumentSnapshot post = await Firestore
    .instance
    .collection('posts')
    .document(postId).get(); // Here postId is the id of the post you are accessing

// You can then get the ids list
final ids = post.data['likes'];

// Get the list of ids that has value = true
final userIds = ids.keys.map((e) => ids[e] == true).toList();

You can store likes as an array instead of Map for convenient data handling, since this likes field only store those that liked the post according to the name.


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

...