【问题标题】:How to send Map<String,bool> to cloud Firestore in Flutter如何在 Flutter 中将 Map<String,bool> 发送到云 Firestore
【发布时间】:2021-04-22 12:21:51
【问题描述】:

我们有一个 Map 声明如下:

Map<String, bool> _selection = {};

它包含如下数据:

key = 'Messi'

value = 'true'

我们想发送包含在 Map _selection 中的数据。

现在我们正在尝试使用如下定义的方法:

Future<Map<String, dynamic>> votedown() async {
      
_selection.forEach((key, value) {

        Map<dynamic, dynamic> comdata = <dynamic, dynamic>{

         'criteriaName': key,
          'isChecked': value,
        };
        return comdata;

      });
    }

我们将数据发送到 Firestore:

 DocumentReference ref =
        FirebaseFirestore.instance.collection('posts').doc();
 
    await ref.set({
      'Players': {
        'Football': {
         await votedown(),
        }
      }
    });

但它给出的错误是:

ArgumentError (Invalid argument: Instance of '_CompactLinkedHashSet<Map<String, dynamic>>')

请帮我解决这个问题?

Firestore 中“帖子”集合中的所需输出:

--> Players

 --> Football

  --> PlayerName : Messi

      isStriker : true

【问题讨论】:

  • 你能解释一下你想做什么(投票、球员、前锋)吗? ?然后我会帮助你更好地设计 firebase,因为即使你的架构也不是最好的。
  • 我们在一个列表视图中获取玩家名称的详细信息,该列表视图存储为 Firestore 中的地图,并且 UI 中的每个条目都有一个复选框。现在用户可以通过选中复选框并点击继续按钮来选择任何玩家条目。我们想在点击继续按钮时将选定的条目存储在 Firestore 中。
  • 好的,对于该任务,您可以首先通过创建 Player 集合开始。在那里,您将包括有关每个玩家的详细信息。然后你有User集合,在里面你可以有一个列表[selectedPlayers] ,你可以在其中包含来自其他集合的玩家的UID。把整个问题详细写下来给我ping一下,我可以给个答案
  • @Tree 所以现在的集合将是:- Players 'PlayerInfoMap' : "playerName" : Messi "isStriker" : true uid: "some random String" 其他集合将是 "Users": "SelectedPlayer ”:“球员姓名”:梅西“isStriker”:真实。但我的问题是什么是 firestore 查询以及我们将如何从复选框 listile 中获取数据。?

标签: flutter dictionary google-cloud-firestore


【解决方案1】:

看来问题出在await downvote() 周围的括号{} 中。由于该方法已经返回了一个地图对象,如果您放入括号中,您将拥有一组地图嵌套对象。此类对象不在 Firestore (reference) 中可用的类型中,这是错误的原因。

我没有操场来测试它,但请尝试以下操作:

 DocumentReference ref =
        FirebaseFirestore.instance.collection('posts').doc();
 
    await ref.set({
      'Players': {
        'Football': await votedown(),
      }
    });

【讨论】:

  • @Akshay 这个answer your question?
  • @WytrzymałyWiktor 它确实保存在 firestore 中,但在 Football 下,它保存为 null。下面是我的 votedown() 函数:- Future> votedown() async { _selection.forEach((key, value) { Map comdata = { 'playerName' : key, 'isStriker': value, }; return comdata; });为什么它不发送值,即使我在调试它时获得了键值。?
  • 我们可以通过在 forEach 循环中循环映射数据并将键和值保存在 List> 中来保存数据。然后传递键值对并添加到 List> 中,然后将其返回到 firestore。
猜你喜欢
  • 2018-10-14
  • 2021-02-11
  • 2019-07-11
  • 2020-08-22
  • 2022-11-12
  • 1970-01-01
  • 2021-11-11
  • 2019-04-09
  • 1970-01-01
相关资源
最近更新 更多