Flutter:如何在下面的文档中检索字段

时间:2020-06-02 10:17:54

标签: android flutter dart google-cloud-firestore

我想在所附图像中显示的所有这些文档中检索hasVoted字段(我在所有创建的文档中都具有hasVoted字段)。以下是我的代码,但似乎无法正常工作。我在做什么错?检索这些字段的正确方法是什么?

_checkUserVotes(context, data) async {

bool hasVoted = false;
final record = Record.fromSnapshot(data);

getData() async {

return await Firestore.instance.collection("finance").getDocuments();

}

getData().then((val){

if(hasVoted == true){

  return Text("Sorry you have voted already");
}

else if(hasVoted != true){

 return GestureDetector(

    onTap: () => record.reference.updateData({'votes': record.votes + 1}),

  );
}
});
}

this is the Backened

1 个答案:

答案 0 :(得分:1)

假设您的集合称为财务,则需要访问每个文档内的Traceback (most recent call last): File "c:/Users/Luca/Desktop/Python/own/playaudio.py", line 3, in <module> playsound('alarmsound.wav') File "C:\Users\Luca\AppData\Local\Programs\Python\Python38-32\lib\site-packages\playsound.py", line 35, in _playsoundWin winCommand('open "' + sound + '" alias', alias) File "C:\Users\Luca\AppData\Local\Programs\Python\Python38-32\lib\site-packages\playsound.py", line 31, in winCommand raise PlaysoundException(exceptionMessage) playsound.PlaysoundException: Error 275 for command: open "alarmsound.wav" alias playsound_0.7197283376226026 Cannot find the specified file. Make sure the path and filename are correct. 字段。

Map

现在的事情是,我不知道您从何处获得该QuerySnapshot snapshot = await Firestore.instance.collection("finance").getDocuments(); snapshot.documents.forEach((document){ if(document.data != null && document.data['hasVoted']['yourId']){ print('Has voted'); } }); 属性,但您现在应该知道。

如果要从它们中迭代,只需直接从该映射中使用值即可。

yourID

但是我真的建议您将数据解析为模型(例如snapshot.documents.forEach((document){ if(document.data['hasVoted'] is Map && (document.data['hasVoted'] as Map).values.first){ print('Has voted'); } }); ),这样才能真正提高代码的可读性。