【问题标题】:What exactly means the snapshot in a Futurebuilder in Flutter?Flutter 中 Futurebuilder 中的快照究竟意味着什么?
【发布时间】:2021-08-01 12:57:49
【问题描述】:

我不确定我在 Flutter 中的快照功能是否正确。 因此,我想问问你们是否同意我对快照的看法。

假设我有下面的 FutureBuilder:

 FutureBuilder(
     future: someFutureFunction(),
     builder: (context, snapshot) {
     if (snapshot.connectionState == ConnectionState.waiting)
         return Center(child: CircularProgressIndicator());
     else
         return Text(counter.toString());
}),

例如someFutureFunction() 返回Future<String>... 是否需要FutureBuilder(builder: (context, snapshot){} 中回调内的snapshot 才能访问someFutureFunction() 的返回值(Future<String>)??

我只是想确定我是否正确,然后再将错误信息存入我的脑海:)

谢谢

【问题讨论】:

    标签: flutter snapshot flutter-futurebuilder


    【解决方案1】:

    快照在内部以AsyncSnapshot 的形式工作,它随指定的 [connectionState] 而变化,并且可以选择 [data] 或 [error] 和可选的 [stackTrace](但不是数据和错误)。

    详情请看以下代码:

      /// Creates an [AsyncSnapshot] in [ConnectionState.none] with null data and error.
      const AsyncSnapshot.nothing() : this._(ConnectionState.none, null, null, null);
    
      /// Creates an [AsyncSnapshot] in [ConnectionState.waiting] with null data and error.
      const AsyncSnapshot.waiting() : this._(ConnectionState.waiting, null, null, null);
    
      /// Creates an [AsyncSnapshot] in the specified [state] and with the specified [data].
      const AsyncSnapshot.withData(ConnectionState state, T data): this._(state, data, null, null);
    
      /// Creates an [AsyncSnapshot] in the specified [state] with the specified [error]
      /// and a [stackTrace].
      ///
      /// If no [stackTrace] is explicitly specified, [StackTrace.empty] will be used instead.
      const AsyncSnapshot.withError(
        ConnectionState state,
        Object error, [
        StackTrace stackTrace = StackTrace.empty,
      ]) : this._(state, null, error, stackTrace);
    

    如果异步计算接收到的最新数据不为空,则[hasData] 将为真。 如果异步计算从未返回值,这可能是 设置为相关小部件指定的初始数据值。

    最后,我们可以说快照作为一种改变其状态的异步计算工作。

    【讨论】:

    • 感谢您的详细回答,但实际上这不是我问题的确切答案。我的问题更多关于参数“快照”。不过还是谢谢
    • snapshot 只是一个名字,你可以用任何东西代替它。我的回答准确地解释了快照是如何工作的,这对于使用快照概念的任何地方都是一样的
    • @Ayrix:如果有道理,你能接受答案吗?
    【解决方案2】:

    快照是您的数据的包装器,具有一些有用的属性。它提供了您的连接状态,以便您可以根据状态更改来确定和更新您的视图。如果您将它用于任何网络调用,您可以更好地理解它。 ConnectionStates 可以是以下任何一种。

    活跃

    Connected to an active asynchronous computation.
    For example, a Stream that has returned at least one value, but is not yet done.
    

    完成

    Connected to a terminated asynchronous computation.
    

    Not currently connected to any asynchronous computation.
    For example, a FutureBuilder whose FutureBuilder.future is null.
    

    等待

    Connected to an asynchronous computation and awaiting interaction.
    

    此外,您在获取数据时可能会收到任何错误。

    这是一个处理其他情况的 FutureBuilder 的更好示例...

    FutureBuilder<String>(
         future: someFutureFunction(),
         builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
         if (snapshot.connectionState == ConnectionState.waiting)
             return Center(child: CircularProgressIndicator());
         else if (snapshot.hasData)
             return Text(snapshot.data);
         else if (snapshot.hasError)
             return Text('Error occured!');
    
    })
    

    您可以从此处找到有关快照的更多信息 https://api.flutter.dev/flutter/widgets/AsyncSnapshot-class.html

    【讨论】:

    • 我其实知道你上面列出的几点。但是你还没有回答我的问题。不过还是谢谢。我的问题更多是关于参数“快照”
    • 我认为这是对您问题的回答。实际上是详尽的。下次再简洁点。在您的情况下,您可以编写您所知道的内容,然后在参数中究竟是什么问题。我还是想知道参数中的含糊之处。
    【解决方案3】:

    快照是与您的 API 的最新交互 例如,如果您刚刚发送了请求,直到答案到来,connectionState 处于等待状态,如果响应到来,快照中的数据将被填充......并且您提出的问题和答案是肯定的

    【讨论】:

    • 感谢您的回复。所以这意味着我对快照的假设(如上所述)是正确的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 2015-10-29
    • 2012-10-10
    • 2011-11-08
    相关资源
    最近更新 更多