firebase 如何倾听一个文档的更改

5us2dqdw  于 2023-01-18  发布在  其他
关注(0)|答案(5)|浏览(92)

我只想在一个文档被更改或更新时得到通知。每个获取更新的示例都总是使用集合。我尝试只使用一个文档来实现这一点,但它从来没有得到任何更新。以下是我现在遇到的不起作用的问题:

@override
Widget build(BuildContext context) {
StreamBuilder<DocumentSnapshot>(
    stream: Firestore.instance
        .collection("users")
        .document(widget.uid)
        .snapshots(),
    builder:
        (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
      user = snapshot.data.data as User;
    });

我已经调试了100次了,但它从来没有到达“builder:“部分。顺便说一下,这不是文档引用的问题。我在这里做错了什么?

6bc51xsx

6bc51xsx1#

这里有一个例子

Firestore.instance
        .collection('Users')
        .document(widget.uid)
        .snapshots()
        .listen((DocumentSnapshot documentSnapshot) {

      Map<String, dynamic> firestoreInfo = documentSnapshot.data;

      setState(() {

        money = firestoreInfo['earnings'];

      });

    })
        .onError((e) => print(e));

但你做错的是

(BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
      user = snapshot.data.data as User;
    });

将其替换为

(BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
      var firestoreData = snapshot.data;

      String info = firestoreData['info'];
    });
abithluo

abithluo2#

这是我的经验和工作很好。(StreamBUilderBLoC模式)。
步骤1 =〉按查询和限制筛选

var userQuery = Firestore.instance
          .collection('tbl_users')
          .where('id', isEqualTo: id)
          .limit(1);

步骤2 =〉倾听

userQuery.snapshots().listen((data) {
            data.documentChanges.forEach((change) {
              print('documentChanges ${change.document.data}');
            });
          });

BLoC

class HomeBloc {
  final userSc = StreamController<UserEntity>(); 

  Future doGetProfileFireStore() async {
    await SharedPreferencesHelper.getUserId().then((id) async {
      print('$this SharedPreferencesHelper.getUserId() ${id}');
      var userQuery = Firestore.instance
          .collection('tbl_users')
          .where('id', isEqualTo: id)
          .limit(1);
      await userQuery.getDocuments().then((data) {
        print('$this userQuery.getDocuments()');
        if (data.documents.length > 0) {
          print('$this data found');
          userQuery.snapshots().listen((data) {
            data.documentChanges.forEach((change) {
              print('documentChanges ${change.document.data}');
              userSc.sink.add(new UserEntity.fromSnapshot(data.documents[0]));
            });
          });
        } else {
          print('$this data not found');
        }
      });
    });
  }

  void dispose() {
    userSc.close();
  } 
}

查看

new StreamBuilder(
        stream: bloc.userSc.stream,
        builder: (BuildContext context, AsyncSnapshot<UserEntity> user) {
          return new Center(
            child: new Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                user.hasData
                    ? new Container(
                        width: 80,
                        height: 80,
                        decoration: new BoxDecoration(
                          borderRadius: BorderRadius.circular(100.0),
                          image: new DecorationImage(
                            image: NetworkImage(user.data.photo),
                            fit: BoxFit.cover,
                          ),
                        ),
                      )
                    : new Container(
                        width: 50,
                        height: 50,
                        child: new CircularProgressIndicator(
                          strokeWidth: 2,
                          valueColor:
                              AlwaysStoppedAnimation<Color>(ColorsConst.base),
                        ),
                      ),
                new Container(
                  margin: EdgeInsets.fromLTRB(10, 0, 0, 0),
                  child: new Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                      new Text(
                        user.hasData
                            ? '${user.data.username.toUpperCase()}'
                            : 'loading',
                        style: TextStyleConst.b16(
                            color: Colors.black
                                .withOpacity(user.hasData ? 1.0 : 0.2),
                            letterSpacing: 2),
                      ),
                      new Container(
                        margin: EdgeInsets.all(5),
                      ),
                      new Text(
                        user.hasData ? '${user.data.bio}' : 'loading',
                        style: TextStyleConst.n14(
                            color: Colors.black
                                .withOpacity(user.hasData ? 1.0 : 0.2)),
                      ),
                      new Container(
                        margin: EdgeInsets.fromLTRB(0, 10, 0, 0),
                        padding: EdgeInsets.all(10),
                        decoration: new BoxDecoration(
                          color: Colors.blue,
                          borderRadius: BorderRadius.circular(100.0),
                        ),
                        child: new Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            new Row(
                              children: <Widget>[
                                new Icon(
                                  Icons.trending_up,
                                  color: Colors.white,
                                  size: 20,
                                ),
                                new Text(
                                  '145K',
                                  style:
                                      TextStyleConst.b14(color: Colors.white),
                                ),
                              ],
                            ),
                            new Container(
                              margin: EdgeInsets.fromLTRB(10, 0, 10, 0),
                            ),
                            new Row(
                              children: <Widget>[
                                new Icon(
                                  Icons.trending_down,
                                  color: Colors.white,
                                  size: 20,
                                ),
                                new Text(
                                  '17',
                                  style:
                                      TextStyleConst.b14(color: Colors.white),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          );
        },
      ),
qvsjd97n

qvsjd97n3#

String collPath = 'books';
String docPath= 'auNEAjG276cQ1C9IUltJ';

DocumentReferance documentReferance = firestoreInstance.collection(collPath).document(docPath);

documentReference.snapshots().listen((snapshot) {
  print(snapshot.data);
}
nom7f22z

nom7f22z4#

下面是使用StreamBuilder的解决方案:

StreamBuilder(
        stream: Firestore.instance
            .collection("sightings")
            .doc(sighting.sightingID)
            .snapshots(),
        builder: (context, snapshot) {
          sighting = Sighting.fromMap(snapshot.data.data()); // Gives you the data map
          return Scaffold(
            appBar: AppBar(
              title: Text('Document Details'),
            ),
            body: Column(
                children: [
                  Text(sighting.type)
                ],
              ),
          );
        });

关键是snapshot.data.data()行,它返回文档中的数据Map。

smdnsysy

smdnsysy5#

使用null safety更新2023

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: StreamBuilder<DocumentSnapshot<Map<String, dynamic>>>(
          stream:  FirebaseFirestore
                   .instance
                   .collection('users')
                   .doc(widget.uid) 
                   // .doc(FirebaseAuth.instance.currentUser!.uid) // 👈 Use this if you want to listen to the present user's document
                   .snapshots(), 
          builder:
              (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
            if (snapshot.hasError) {
              return const Text('Something went wrong');
            }
            if (snapshot.connectionState == ConnectionState.waiting) {
              return const Text("Loading");
            }
            Map<String, dynamic> data =
                snapshot.data!.data()! as Map<String, dynamic>;
            return Text(data['fullName']); // 👈 your valid data here
          },
        ),
      ),
    );
  }

另请参阅:How to use StreamBuilder and FutureBuilder for single and multiple documents

相关问题