如何在Flutter中的列表视图中显示Firestore数据

时间:2020-06-13 21:29:05

标签: firebase flutter

我正试图从我的Firebase数据库中显示用户的基本信息列表。

database image1

database image2

database image3

我知道如何显示Users集合的所有用户,但是我不知道如何在列表视图中提取每个用户的basicInfo。

  final CollectionReference userCollection = 
  Firestore.instance.collection('Users');

class FirebaseFirestoreService {

  static final FirebaseFirestoreService _instance = new 
  FirebaseFirestoreService.internal();

  factory FirebaseFirestoreService() => _instance;

  FirebaseFirestoreService.internal();

  Stream<QuerySnapshot> getUserList({int offset, int limit}) {
    Stream<QuerySnapshot> snapshots = userCollection.snapshots();

    if (offset != null) {
      snapshots = snapshots.skip(offset);
    }

    if (limit != null) {
      snapshots = snapshots.take(limit);
    }

    return snapshots;
  }
}

@override
  void initState() {
    super.initState();

    items = new List();

    cardSub?.cancel();
    cardSub = db.getUserList().listen((QuerySnapshot snapshot) {
      final List<CardModel> cards = snapshot.documents
          .map((documentSnapshot) => CardModel.fromMap(documentSnapshot.data))
          .toList();

      setState(() {
        this.items = cards;
      });
    });
  }

如何在列表视图中显示单个用户的basicInfo?

0 个答案:

没有答案