如何使用StreamBuilder观察Flutter中的Firebase变化

ws51t4hk  于 2022-12-19  发布在  Flutter
关注(0)|答案(1)|浏览(119)

我正在尝试观察Firebase集合的变化。在第一次运行时,列表视图没有构建。在Firebase上更改数据后,列表视图正确呈现,但使用了以前的快照,因为我在屏幕上看到了旧数据。
有办法找出错误吗?

@override
  Widget build(BuildContext context) {
    return BlocListener<VoteCubit, VoteState>(
      listener: (context, state) {
        if (state.status == FormStatus.success) {
          print("Success");
        } else if (state.status == FormStatus.inprogress) {
          print("In progress");
        } else if (state.status == FormStatus.fail) {
          print("fail");
        } else {
          print("Empty Area");
        }
      },
      child: StreamBuilder<QuerySnapshot>(
        stream: FirebaseFirestore.instance.collection('liveSurvey').snapshots(),
        builder: (context, snapshot) {
          final voteCubit = context.read<VoteCubit>();
          final voteState = voteCubit.state;
          if (snapshot.hasData) {
            voteCubit.getAllRooms(snapshot);
            print("Here");
            return ListView.builder(
              itemCount: voteState.rooms.length,
              itemBuilder: (context, i) {
                print("Inside Listview");
                . . .
9gm1akwq

9gm1akwq1#

使用像这样的流构建器真实的数据。

child: StreamBuilder<QuerySnapshot>(
        stream: FirebaseFirestore.instance
            .collection("travellers/India/details")
            .where(
              'firestore_id',
              isEqualTo: widget.get_firestore_id,
            )
            .snapshots(),
        builder:
            (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {

if (snapshot.connectionState == ConnectionState.waiting) {
    print('play lodader')
}else if (snapshot.hasData) {
print('your_ui_here')
}

相关问题