AngularDart FireStore-如何“调用”单个文档

时间:2018-10-19 18:58:40

标签: firebase web-applications angular5 google-cloud-firestore angular-dart

我有一个仪表板(类似于AngularDart英雄之旅),该仪表板基于FireStore中的集合创建按钮网格。单击该按钮后,会将您带到单个项目,以查看/编辑详细信息。

我无法在该组件内部获取正确的“ id”值,才能从Firestore中提取该文档的数据。

我从Firestore调用单个文档的方法正确吗?它返回null。

据我所知,问题出在FirebaseService中,该问题负责调用Firebase并编译“英雄”列表 具体来说,get(int id)函数似乎并未像预期的那样呼叫英雄。

@Injectable()
class FirebaseService {

List<Hero> heroes = [];
List<Hero> hero =[];

getId(id) {
fs.Firestore store = firestore();
fs.DocumentReference docRef = store.collection('items').doc('id');

docRef.onSnapshot.listen((querySnapshot) {
  num number = querySnapshot.get('id');
  var name = querySnapshot.get('name');
  bool full = querySnapshot.get('pickupNeeded');
  var url = querySnapshot.get('id').toString();
  var item = new Hero(number, name, full, url);
  hero.insert(0, item);
  return hero;
});

}

  callFirestore() {

    fs.Firestore store = firestore();
    fs.CollectionReference ref = store.collection("items");


    ref.onSnapshot.listen((querySnapshot) {
      querySnapshot.docChanges().forEach((change) {
        if (change.type == "added") {
          var docSnapshot = change.doc;
          var id = docSnapshot.data()['id'];
          var name = docSnapshot.data()["name"];
          var full = docSnapshot.data()["pickupNeeded"];
          var url = docSnapshot.data()["id"].toString();
          print(id);
          print(name);
          print(full);
          var item = new Hero(id, name, full, url);
          heroes.insert(0, item);
        }
      });
    });
   }

  The function returns null. 

0 个答案:

没有答案