简体   繁体   English

Flutter 中的 StreamBuilder 卡在 ConnectionState.waiting 中,仅显示加载标记

[英]StreamBuilder in Flutter stuck with ConnectionState.waiting and displays only the loading mark

Hi I am trying to display the data inside the Firebase documents into my Flutter dynamically where they get rendered using a loop, so I made a List<Widget> Cards and added to it the function makeItem() that contains the cards, and put them inside a loop, so the problem is that when I run the code it outputs print(snapshot.connectionState);嗨,我正在尝试将 Firebase 文档中的数据动态显示到我的 Flutter 中,它们使用循环进行渲染,所以我制作了一个List<Widget> Cards并向其中添加了包含卡片的 function makeItem() ,并将它们放入在一个循环中,所以问题是当我运行代码时它输出print(snapshot.connectionState); as ConnectionState.waiting all the time and it should be async snapshot yet it refuses to load the data as required, I should mention that the data is display as wanted when I hit "Hot reload in Android Studio".因为ConnectionState.waiting一直在等待,它应该是异步快照,但它拒绝按要求加载数据,我应该提到当我点击“在 Android Studio 中热重载”时,数据是按需要显示的。 so I don't know how resolve this issue.所以我不知道如何解决这个问题。 Thanks in Advance提前致谢

Seems this is an old subject but for people still looking for solution.似乎这是一个古老的主题,但对于仍在寻找解决方案的人来说。 I had the same problem when using STREAM BUILDER with PROVIDER&CHANGE NOTIFIER.将 STREAM BUILDER 与 PROVIDER&CHANGE NOTIFIER 一起使用时,我遇到了同样的问题。 When returning back to the view, one should re-assign the stream itself.返回视图时,应重新分配 stream 本身。 Make a get function for your stream and in that function before returning your stream re-assign the stream. Make a get function for your stream and in that function before returning your stream re-assign the stream. That solved the problem of loading issue for me.这为我解决了加载问题。 Sorry for my bad explanation skills, hope helps someone:)抱歉我的解释能力不好,希望对某人有所帮助:)

Can you try the following?你可以试试下面的吗?

class MyList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      stream: _firestore.collection(widget.city).snapshots(),
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (snapshot.hasError)
          return Text('Error: ${snapshot.error}');
        switch (snapshot.connectionState) {
          case ConnectionState.waiting: return Center(child: CircularProgressIndicator(backgroundColor: Colors.amber,strokeWidth: 1),),
          default:
            return ListView(
              children: snapshot.data.documents.map((DocumentSnapshot document) {
                return makeItem(
                  pointName: document['name'],
                  huge: document['lastname'],
                  moderate: document['mobileNumber'],
                  none: document['location'],
                  fights: document['job'],
               );
              }).toList(),
           );
        }
      },
    );
  }
}

I think I got something for you try this out.我想我得到了一些东西给你试试这个。 It works on my emulator.它适用于我的模拟器。

List<Widget> cards = [];
  Stream<QuerySnapshot> firebaseStream;

  @override
  void initState() {
    super.initState();
    firebaseStream = Firestore.instance.collection('Hearings').snapshots();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: StreamBuilder<QuerySnapshot>(
          stream: firebaseStream,
          builder: (BuildContext context,
              AsyncSnapshot<QuerySnapshot> asyncSnapshot) {
            List<DocumentSnapshot> snapData;

            if (asyncSnapshot.connectionState == ConnectionState.waiting) {
              return Container(
                child: Center(
                  child: CircularProgressIndicator(
                    backgroundColor: Colors.amber,
                    strokeWidth: 1,
                  ),
                ),
              );
            } else if (asyncSnapshot.connectionState ==
                ConnectionState.active) {
              snapData = asyncSnapshot.data.documents;
              if (asyncSnapshot.hasData) {
                for (int i = 0; i < snapData.length; i++) {
                  Widget card = Text(snapData[i].data['locationName']);

                  cards.add(card);
                }
              }
            }
            return ListView.builder(
              itemCount: cards.length,
              itemBuilder: (context, index) => cards[index],
            );
          },
        ),
      ),
    );

I got bad news too though now that the data is updating it exposed some flaws in your logic its duplicating old entries in your array.我也得到了坏消息,虽然现在数据正在更新,但它暴露了你的逻辑中的一些缺陷,它复制了数组中的旧条目。 You'll see.你会看到的。 That should be easy to fix though.不过,这应该很容易解决。

Please use FutureBuilder It solved my problem.请使用 FutureBuilder 它解决了我的问题。

Using stream.cast() on StreamBuilder solved my problem.在 StreamBuilder 上使用 stream.cast() 解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM