首页 > 解决方案 > “Query”类型不是“CollectionReference”类型的子类型

问题描述

我正在尝试按 date_posted 对我的数据进行排序,但我得到'类型'查询'不是类型'CollectionReference'的子类型。' 我曾尝试寻找解决方案,但都是徒劳的!我的代码:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:kopala_dictionary/models/words.dart';

class DatabaseService {
  //cloud databse colection

  final CollectionReference wordsCollection =
      Firestore.instance.collection('words').orderBy('date_posted');

  Future insertData(String word, String english_translation,
      String bemba_translation, String user_id, DateTime date_posted) async {
    return await wordsCollection.document().setData({
      'word': word,
      'english_translation': english_translation,
      'bemba_translation': bemba_translation,
      'user_id': user_id,
      'date_posted': date_posted
    });
  }

  //words list from snappshots

  List<Words> _wordsFromSnapShots(QuerySnapshot snapshot) {
    return snapshot.documents.map((doc) {
      return Words(
        word: doc.data['word'],
        englishTranslation: doc.data['english_translation'],
        bembaTranslation: doc.data['bemba_translation'],
      );
    }).toList();
  }

  //Stream snapshots

  Stream<List<Words>> get words {
    return wordsCollection.snapshots().map(_wordsFromSnapShots);
  }
}

当我将其更改为:

final CollectionReference wordsCollection = Firestore.instance.collection('words').orderBy('date_posted');

我得到'类型'查询'不是'CollectionReference'类型的子类型'

标签: firebasefluttergoogle-cloud-firestore

解决方案


改变这个

 final CollectionReference wordsCollection =
  Firestore.instance.collection('words').orderBy('date_posted');

进入这个

    final Query wordsCollection =
  Firestore.instance.collection('words').orderBy('date_posted');

推荐阅读