【问题标题】:How to skip null/empty variables in the firestore collection?如何跳过firestore集合中的空/空变量?
【发布时间】:2018-12-15 10:04:17
【问题描述】:

像 firebase 实时数据库一样,我不想在 firestore 集合中存储 null/空值。所以我如何从 firestore 集合中跳过或删除 null 字段

下面的函数将数据保存到firestore

private void saveTofireStore(String path, User userObject,Class<?> classType){

    FirebaseFirestore db = FirebaseFirestore.getInstance();
    documentReference = db.document(path);
    Log.d(TAG,""+documentReference.getPath());
   documentReference
            .set(userObject)
            .addOnCompleteListener(task -> {
                if (task.isSuccessful()){
                  appearanceMutableLiveData.setValue(task.getResult());
                }else {
                    appearanceMutableLiveData.setValue(null);
                }
            });

}

【问题讨论】:

    标签: android firebase google-cloud-firestore


    【解决方案1】:

    我找到了一种使用 Gson 序列化对象然后转换为地图的简单方法,然后您可以保存该对象

    private Map<String, Object> removeNullValues(User userObject) {
        Gson gson = new GsonBuilder().create();
    
        Map<String, Object> map = new Gson().fromJson(
                gson.toJson(userObject), new TypeToken<HashMap<String, Object>>() {
                }.getType()
        );
    
        return map;
    }
    

    然后

    documentReference
    .set( removeNullValues( userObject) )
    .addOnSuccessListener {}
    

    如果您使用的是 proguard,请确保添加此规则

    -keepclassmembers enum * { *; }
    

    【讨论】:

    【解决方案2】:

    我的解决方案是添加一个名为 toFirestoreJson 的方法。

    import 'package:cloud_firestore/cloud_firestore.dart';
    
    class User {
      String documentID;
      final String name;
      final String city;
      final String postalCode;
      final String state;
      final String address;
      final List<String> emails;
    
      User(
          {this.name,
          this.city,
          this.postalCode,
          this.state,
          this.address,
          this.emails});
    
      factory User.fromFirestore(DocumentSnapshot documentSnapshot) {
        User data = User.fromJson(documentSnapshot.data);
        data.documentID = documentSnapshot.documentID;
        return data;
      }
    
      factory User.fromJson(Map<String, dynamic> json) => User(
            name: json["name"] == null ? null : json["name"],
            city: json["city"] == null ? null : json["city"],
            postalCode: json["postal_code"] == null ? null : json["postal_code"],
            state: json["state"] == null ? null : json["state"],
            address: json["address"] == null ? null : json["address"],
            emails: json["emails"] == null
                ? null
                : List.castFrom(json["emails"]),
          );
    
      Map<String, dynamic> toFirestoreJson() {
        Map json = toJson();
        json.removeWhere((key, value) => value == null);
        return json;
      }
    
      Map<String, dynamic> toJson() => {
            "name": name == null ? null : name,
            "city": city == null ? null : city,
            "postal_code": postalCode == null ? null : postalCode,
            "state": state == null ? null : state,
            "address": address == null ? null : address,
            "emails" : emails == null ? null : emails,
          };
    }
    

    【讨论】:

    • 我们都知道我们可以编写自己的函数,但是在这里我们已经有了工具,为什么不直接使用它们
    • 我不知道....这应该是公认的答案
    • 为什么应该接受这个答案?如果您仔细查看用 java/kotlin 而不是 fultter 编写的 qustion,那么 ans 应该在 java/koltin
    猜你喜欢
    • 2013-08-30
    • 2020-01-11
    • 2019-09-07
    • 1970-01-01
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    相关资源
    最近更新 更多