简体   繁体   English

如何在 Flutter StreamBuilder 中使用 hasData、hasError 和 ConnectionState 处理 snapshot.data

[英]How to handle snapshot.data with hasData, hasError, and ConnectionState in Flutter StreamBuilder

I'm not sure about how to handle Flutter StreamBuilder.我不确定如何处理 Flutter StreamBuilder。 I found 3 cases and want to know which one would be better than others with any reason.我找到了 3 个案例,想知道出于任何原因,哪一个会比其他案例更好。 Or if there is a better way to handle it other than these cases.或者,除了这些情况之外,是否还有更好的方法来处理它。

Also, what is the difference between ConnectionState.active and ConnectionState.done ?另外, ConnectionState.activeConnectionState.done之间有什么区别? Should I wait using snapshot.data until ConnectionState.done or not ( Case 1 Or Case 2 ).我是否应该等待使用snapshot.data直到ConnectionState.done案例 1案例 2 )。

Case 1:情况1:

    return StreamBuilder(
      builder: (context, snapshot) {
        if (snapshot.hasError) return Text('Error');
        switch (snapshot.connectionState) {
          case ConnectionState.none:
          case ConnectionState.waiting:
            return CircularProgressIndicator();
          case ConnectionState.active:
          case ConnectionState.done:
            return Text('${snapshot.data}');
        }
        return null;
      },
    );

Case 2:案例2:

    return StreamBuilder(
      builder: (context, snapshot) {
        if (snapshot.hasError) return Text('Error');
        switch (snapshot.connectionState) {
          case ConnectionState.none:
          case ConnectionState.waiting:
          case ConnectionState.active:
            return CircularProgressIndicator();
          case ConnectionState.done:
            return Text('${snapshot.data}');
        }
        return null;
      },
    );

Case 3:案例3:

    return StreamBuilder(
      builder: (context, snapshot) {
        if (snapshot.hasError) return Text('Error');
        if (!snapshot.hasData) return CircularProgressIndicator();
        return Text('${snapshot.data}');
      },
    );

According to Flutter Docs it states the the difference between active and done is that ConnectionState.active : is the Stream that its not done yet but returned at least one value,根据Flutter Docs,它指出 active 和 done 之间的区别在于ConnectionState.active :是尚未完成但返回至少一个值的流,
ConnectionState.done : where it has terminated the asynchronous computation/transaction you are dealing with. ConnectionState.done :它终止了您正在处理的异步计算/事务。


regarding your question on when should you wait, It depends on the kind of computation you are doing.关于您何时应该等待的问题,这取决于您正在执行的计算类型。

Normally ConnectionState.active is more important to take care of because you are dealing with a stream which means data could be updated and the state of your app should also update.通常ConnectionState.active更重要,因为您正在处理一个流,这意味着数据可以更新并且您的应用程序的状态也应该更新。

You can use ConnectionState.done to make sure you fetched the data and the asynchronous connection is terminated.您可以使用ConnectionState.done来确保您获取了数据并终止了异步连接。

To get a good idea on how to deal with StreamBuilder and QuerySnapshot check out Firebase for Flutter要获得有关如何处理StreamBuilderQuerySnapshot的好主意,请查看Firebase for Flutter

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

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