Firestore 分页 startAfter(DocumentSnapshot) 在 java 中工作,但在 kotlin 中不起作用

Shi*_*lal 1 pagination android kotlin google-cloud-firestore

我正在尝试将一个 Android Firestore 项目从 java 转换为 kotlin。但陷入了分页部分,其中带有 java 代码的 startAfter(DocumentSnapshot) 工作正常。但 kotlin 只给出前 3 个结果。StartAfter(DocumentSnapshot) 部分不起作用。

如果您能指出我在 Kotlin 中哪里出错了,那将会非常有帮助。

这是运行完美的java代码

 public void loadNotes(View v) {
    Query query;
    if (lastResult == null) {
        query = notebookRef.orderBy("priority")
                .limit(3);
    } else {
        query = notebookRef.orderBy("priority")
                .startAfter(lastResult)
                .limit(3);
    }
    Log.d(TAG, "loadNotes: "+ query);
    query.get()
            .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                    String data = "";
                    for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
                        Notee note = documentSnapshot.toObject(Notee.class);
                        note.setDocumentId(documentSnapshot.getId());
                        String documentId = note.getDocumentId();
                        String title = note.getTitle();
                        String description = note.getDescription();
                        int priority = note.getPriority();
                        data += "ID: " + documentId
                                + "\nTitle: " + title + "\nDescription: " + description
                                + "\nPriority: " + priority + "\n\n";
                    }
                    if (queryDocumentSnapshots.size() > 0) {
                        data += "___________\n\n";
                        textViewData.append(data);
                        lastResult = queryDocumentSnapshots.getDocuments()
                                .get(queryDocumentSnapshots.size() - 1);
                    }
                }
            });
}
Run Code Online (Sandbox Code Playgroud)

这是Kotlin的,它不起作用。

 private fun loadNotes() {
    val query = if (lastResult == null) {
        notebookRef
            .orderBy("priority")
            .limit(3)
    } else {
        Log.d(TAG, "loadNotes: ${lastResult!!.id}")
        notebookRef
            .orderBy("priority")
            .startAfter(lastResult)
            .limit(3)

    }

    Log.d(TAG, "loadNotes: $query")


    query.get()
        .addOnSuccessListener { QuerySnapshot ->
            var text = ""
            for (queryDocumentSnapshot in QuerySnapshot) {
                val note: Note = queryDocumentSnapshot.toObject(Note::class.java)
                note.docID = queryDocumentSnapshot.id

                val title = note.title
                val description = note.description
                val priority = note.priority
                text += "ID: ${note.docID} \n Title : $title\n Description :$description\n" +
                        "Priority :$priority \n"

            }
            if (QuerySnapshot.size() > 0) {
                text += "---------------\n\n"
                textView_data.append(text)
                lastResult = QuerySnapshot.documents[QuerySnapshot.size() - 1]
            }
        }
}
Run Code Online (Sandbox Code Playgroud)

希望能得到一些帮助

谢谢

测试所需代码:JavaActivity KotlinActivity Note ModelActivity_main.xml

小智 6

好吧,在经历了同样的问题之后。通过使用非空强制转换(!!)解决了这个问题。例如:

val query = transactionsCollection
            .orderBy(Field.CREATED, Query.Direction.DESCENDING)
            .startAfter(lastDocument!!)
            .limit(PAGE_SIZE)
Run Code Online (Sandbox Code Playgroud)

在你的情况下,它将是:

 query = notebookRef.orderBy("priority")
                .startAfter(lastResult!!)
                .limit(3);
Run Code Online (Sandbox Code Playgroud)

它正在 Kotlin 上工作。