如何在Firestore的查询中使用循环?

时间:2019-11-17 14:47:00

标签: firebase flutter dart google-cloud-firestore

是否可以在查询中使用循环?

  Future<void> addServiceConfig(String uid, List<ServiceConfigModel> model) {
    _db.collection('users').document(uid).updateData({
      'businessDetails':{
        'serviceConfig':FieldValue.arrayUnion([
          {
            for(int i = 0;i <model.length;i++){
              if (model[i].inShop != null) 'inShop': model[i].inShop,
              if (model[i].inShopAndClientLocation != null)
                'inShopAndClientLocation': model[i].inShopAndClientLocation,
              if (model[i].clientLocation != null)
                'clientLocation': model[i].clientLocation,
              'serviceLocations': model[i].serviceLocations,
              'subCategoryId': model[i].subCategoryId
            }
        }
        ])
      }
    });
  }

我收到此错误:

  

[ERROR:flutter / lib / ui / ui_dart_state.cc(148)]未处理的异常:无效的参数:'_CompactLinkedHashSet>的实例

1 个答案:

答案 0 :(得分:1)

您不能在表达式内使用循环(或许多其他语句),例如字段的值。一种解决方案是将构造值拉到rand()之外,然后传递给它:

update()

var result = new List();

//var config = new Map()