Lookup
Lookup

Reputation: 21

how to resolve flutter DocumentSnapshot problem?

this is my code

follower_api.dart

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../help/fire_path.dart';

class FollowerApi extends GetxController {
  static FollowerApi get to => Get.find();

  final CollectionReference _ref =
      FirebaseFirestore.instance.collection(FirePath.followerFirePath);

  Stream<DocumentSnapshot> fetchColData(String uid) =>
      _ref.doc(uid).snapshots();
}

follower_ctrl.dart

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:get/get.dart';
import 'package:lookup/core/model/follower_info.dart';
import 'package:lookup/core/service/follower_api.dart';


class FollowerCtrl extends GetxController {
  final FollowerApi _followerApi = FollowerApi.to;

  void fetchFollowerLstByUid(String uid) {
    _followerApi.fetchColData(uid).listen((event) {
      loading(true);
      followerInfoLst(event.docs
          .map<FollowerInfo>((doc) =>
              FollowerInfo.fromMap(doc.id, doc.data() as Map<String, dynamic>))
          .toList());
      loading(false);
    });
  }

  Stream<DocumentSnapshot> fetchColData(String uid) =>
      _followerApi.fetchColData(uid);

  RxList<FollowerInfo> followerInfoLst = <FollowerInfo>[].obs;

  RxBool loading = false.obs;

  static FollowerCtrl get to => Get.find();
}

But an error occurs in the follower_ctrl.dart file.

This is error content

The getter 'docs' isn't defined for the type 'DocumentSnapshot<Object?>'. Try importing the library that defines 'docs', correcting the name to the name of an existing getter, or defining a getter or field named 'docs'.

Upvotes: 0

Views: 194

Answers (1)

Ali Hassan
Ali Hassan

Reputation: 731

 void fetchFollowerLstByUid(String uid) {

_followerApi.fetchColData(uid).listen((event) {
  // that return single document as event it cannot be querysnapshot so 
// you don't have the event.docs getter
// the right way is like this
  loading(true);
  followerInfoLst([
          FollowerInfo.fromMap(event.id, event.data() as Map<String, dynamic>])
      .toList());
  loading(false);
});
 }

if you want to access multiple document that just use _ref.snapshots() this way you have access to querysnapshot and that you can access event.docs getter and apply parsing on it

Upvotes: 2

Related Questions