Firebase 如何在firestore上使用循环内部查询?

Firebase 如何在firestore上使用循环内部查询?,firebase,flutter,dart,google-cloud-firestore,Firebase,Flutter,Dart,Google Cloud Firestore,可以在查询中使用循环吗 Future<void> addServiceConfig(String uid, List<ServiceConfigModel> model) { _db.collection('users').document(uid).updateData({ 'businessDetails':{ 'serviceConfig':FieldValue.arrayUnion([ {

可以在查询中使用循环吗

  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
            }
        }
        ])
      }
    });
  }
我得到了这个错误:

[错误:flatter/lib/ui/ui\u dart\u state.cc148]未处理的异常:无效参数:“\u CompactLinkedHashSet>的实例”


不能在表达式中使用循环或许多其他语句,如字段的值。一种解决方案是在“全部更新”之外构造值,然后将其传入:

//var config = new Map()
var结果=新列表

for(int i = 0;i < model.length; i++){
  var config = new Map();
  if (model[i].inShop != null) config['inShop'] = model[i].inShop;
  if (model[i].inShopAndClientLocation != null) 
    config['inShopAndClientLocation'] = model[i].inShopAndClientLocation;
  if (model[i].clientLocation != null) 
    config['clientLocation'] = model[i].clientLocation;
  config['serviceLocations'] = model[i].serviceLocations,
  config['subCategoryId'] = model[i].subCategoryId;
 result.add(config);
}

_db.collection('users').document(uid).updateData({
  'businessDetails':{
    'serviceConfig': FieldValue.arrayUnion(result)
  }
});

谢谢我试过了。但仅向数据库插入1个值。。模型长度为3。但在数据库中只插入1我认为问题在于,配置变量不是映射的列表