【问题标题】:Argument 'Set<Future<DocumentReference<Map<String, dynamic>>>> Function(String)' can't be assigned to parameter type 'void Function(String, dynamic)'参数 'Set<Future<DocumentReference<Map<String, dynamic>>>> Function(String)' 不能分配给参数类型 'void Function(String, dynamic)'
【发布时间】:2021-06-08 15:53:26
【问题描述】:

我一直在尝试将多个 Firestore 文档从一个集合添加到我的颤振应用程序中的另一个集合中,到目前为止,这是我最接近的一次。

我尝试手动创建从 Firestore 数据读取的地图,然后使用 forEach() 将其存储回另一个集合中的 Firestore,但失败了。

我在下面使用了相同的函数,forEach() 一个带有硬编码数组的函数:

  var myArray69 = [
    {
      'name': 'a',
      'brand': 'bruh'
    },
    {
      'name': 'b'
    }
  ];
  myArray69.forEach((doc) => {     
    FirebaseFirestore.instance.collection('myOtherCollection69').add(doc)});

并且它将每个数组作为文档添加到 Firestore 中就好了,所以我一直在尝试使用来自另一个集合的数据来复制它,但没有运气。

这是我的代码:

Widget _foodList(){
  return Expanded(
      child: StreamBuilder(
        stream: FirebaseFirestore.instance
          .collection('foodShelf')
          .snapshots(),
        builder: buildFoodList,
      ),
  );
}


Widget buildFoodList(BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
  if (snapshot.hasData) {
    return ListView.builder(
      itemCount: snapshot.data.docs.length,
      itemBuilder: (BuildContext context, int index) {

    DocumentSnapshot food = snapshot.data.docs[index];
    
    var foodName = food.get('foodName');

    var someMap = {
      "a": foodName,
      "b": '2',
      };

    return GestureDetector(
      key: Key(food.id),
      onTap: () {
        someMap.forEach((doc) => {      // <-----  First error is here
          FirebaseFirestore.instance.collection('myOtherCollection69').add(doc)});
        print(someMap);
      },
      child: ListTile(
        title: Text(foodName),
      ),
    );
  } 
);
}

如果我注释掉这一部分:

  someMap.forEach((doc) => {
    FirebaseFirestore.instance.collection('myOtherCollection69').add(doc)});

只留下print(someMap);,然后我点击屏幕上的一个结果,我在控制台中得到了地图:

I/flutter ( 6922): {a: Pure 100% florida orange juice, b: 2}

但我无法使用此代码将其添加到 Firestore,我收到以下错误:

  • 第一个错误:
The argument type 'Set<Future<DocumentReference<Map<String, dynamic>>>> Function(String)' can't be assigned to the parameter type 'void Function(String, dynamic)'.dart(argument_type_not_assignable)
  • 第二个错误:
The argument type 'String' can't be assigned to the parameter type 'Map<String, dynamic>'.dart(argument_type_not_assignable)

任何帮助将不胜感激!

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    问题是您的myArray69ListMap&lt;String, String&gt;
    有一个forEach 循环会给出Map 的结果,add(doc) 方法会接受该结果。
    Map 列表给出了一个 Map 项目,如下所示:
    myArray69.forEach(void Function(Map&lt;String, String&gt;) function)

    但是,
    您的 someMap 变量本身就是 Map
    因此,如果您对此使用 forEach,它将为您提供 2 个值。
    一个作为key,另一个作为value 分配给密钥。
    forEach 操作将给出:
    someMap.forEach((key, value) =&gt; print([key, value]));

    所以要回答你的问题,你应该直接添加Map 而不是循环它。 示例:

    onTap: () {
      FirebaseFirestore.instance.collection('myOtherCollection69').add(someMap);
      print(someMap);;
    },
    

    【讨论】:

      【解决方案2】:

      太棒了,在为此苦苦挣扎了几个小时后,我发布了问题并立即弄清楚如何解决我的问题。

      对我的自定义地图进行了此更改并使其正常工作(将文档添加到 firestore):

      (-) var someMap = {
           "a": foodName,
           "b": '2',
          };
      
      (+) List<Map<String, String>> someMap = [
            {
              "a": foodName,
              "b": '2',
            }
          ];
      

      现在我仍在努力将所有文档一次从一个集合添加到另一个集合。

      【讨论】:

        猜你喜欢
        • 2020-12-15
        • 2021-03-23
        • 2020-12-27
        • 2021-05-17
        • 2021-10-22
        • 2021-08-29
        • 2021-10-02
        • 2021-10-24
        相关资源
        最近更新 更多