【问题标题】:cannot resolve method 'arrayUnion' - firestore无法解析方法“arrayUnion”-firestore
【发布时间】:2019-02-09 03:10:21
【问题描述】:

我正在查看firestore 的以下谷歌文档,尤其是在更新数组方面。他们提到:

更新数组中的元素 如果您的文档包含一个数组字段,您可以使用 arrayUnion() 和 arrayRemove() 添加和删除元素。 arrayUnion() 将元素添加到数组中,但只添加不存在的元素。 arrayRemove() 删除每个给定元素的所有实例。

问题是,Android Studio 无法解析方法 arrayUnion。 相关部分代码:

public void onClick(View view) {
    if(isStringValid(newListName.getText().toString())){
        currListName = newListName.getText().toString().trim();
        itemList mList = new itemList(currListName , mAuth.getCurrentUser().getEmail());
        Map<String , Object> listMap = new HashMap<>();
        listMap.put(KEY_LIST_NAME , currListName);
        listMap.put(KEY_OWNER , mAuth.getCurrentUser().getEmail());
        listMap.put(KEY_HAS_ACCESS , Arrays.asList(mAuth.getCurrentUser().getEmail()));
        userRef.update("hasAccess" , FieldValue.arrayUnion(currListName));

分级:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    implementation 'com.android.support:support-v4:28.0.0-rc02'
    implementation 'com.android.support:design:28.0.0-rc01'
    implementation 'com.google.firebase:firebase-core:16.0.3'
    implementation 'com.google.firebase:firebase-auth:16.0.3'
    implementation 'com.google.firebase:firebase-firestore:17.0.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.google.android.gms:play-services-auth:16.0.0'
}

他们的代码:

DocumentReference washingtonRef = db.collection("cities").document("DC");
washingtonRef.update("regions", FieldValue.arrayUnion("greater_virginia"));

【问题讨论】:

    标签: android google-cloud-firestore


    【解决方案1】:

    我在 Firebase Firestore SDK (21.0.0) 中发现了两个 FieldValue 类:

    com.google.firebase.firestore.model.value.FieldValue
    com.google.firebase.firestore.FieldValue
    

    第一个没有 arrayUnion 方法,第二个有。检查您的代码文件中导入了什么类。

    【讨论】:

      【解决方案2】:

      根据发行说明,从客户端 SDK 版本 17.0.5 开始支持数组操作。这是发行说明的link

      无论如何,请更新您的客户端 SDK 以使用最新的 Firestore API。

      【讨论】:

      • 谢谢!通过更新 gradle 构建解决了: implementation 'com.google.firebase:firebase-firestore:17.1.0'
      猜你喜欢
      • 2020-05-25
      • 2019-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-15
      • 2015-06-10
      • 2019-08-28
      相关资源
      最近更新 更多