What exactly means the snapshot in a Futurebuilder in Flutter?

752

Solution 1

snapshot is the most recent interaction with your API for example, if you just have sent your request until the answer comes connectionState is in waiting and if the response comes data in snapshot will be filled....and you asked question and answer to that is yes

Solution 2

snapshot internally works as AsyncSnapshot which changes with the specified [connectionState], and optionally either [data] or [error] with an optional [stackTrace] (but not both data and error).

See below code for more detail:

  /// 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);

If the latest data received by the asynchronous computation is not null then [hasData] will be true. If the asynchronous computation has never returned a value, this may be set to an initial data value specified by the relevant widget.

finally, we can say snapshot works as an asynchronous computation that changes its states.

Solution 3

Snapshot is a wrapper around your data with some useful properties. It provides the state of your connection, so that you can determine and update your view according to state changes. You can understand it better if you use it for any network call. ConnectionStates can be any one of these below.

active

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

done

Connected to a terminated asynchronous computation.

none

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

waiting

Connected to an asynchronous computation and awaiting interaction.

Also, you can receive any error you might have while fetching data.

Here is a better example of FutureBuilder which handles some other cases...

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!');

})

you can find more about snapshot from here https://api.flutter.dev/flutter/widgets/AsyncSnapshot-class.html

Share:
752
Ayrix
Author by

Ayrix

Updated on December 29, 2022

Comments

  • Ayrix
    Ayrix over 1 year

    I'm not sure if I got the snapshot thing in Flutter right. Therefore I would like to ask you guys if you aggree my thoughts about snapshot or not.

    Let's say I have the FutureBuilder below:

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

    For example someFutureFunction() returns Future<String>... Is the snapshot inside the call-back in FutureBuilder(builder: (context, snapshot){} needed to access the returned value (Future<String>) of someFutureFunction() ??

    I just wand to make sure if I got it right before saving wrong information in my mind :)

    Thanks

  • Ayrix
    Ayrix almost 3 years
    Thanks for you detailed answer but it's actually not the exact answer of my question. My question was more about the parameter "snapshot". But thanks anyway
  • Ayrix
    Ayrix almost 3 years
    I actually knew the points you have listed above. But you haven't answered my question. But thanks anyway. My question was more about the parameter "snapshot"
  • Ayrix
    Ayrix almost 3 years
    Thanks for your reply. So that means my assumption about snapshot (explained above) is correct?
  • Jitesh Mohite
    Jitesh Mohite almost 3 years
    snapshot is just a name, you can use anything instead of it. My answer exactly explain how snapshot works, and this is the same for everywhere where the snapshot concept is used
  • Jitesh Mohite
    Jitesh Mohite almost 3 years
    @Ayrix: could you accept the answer if it makes sense?
  • Abdifatah Mohamed
    Abdifatah Mohamed almost 3 years
    I find this as an answer to your question. in fact an exhaustive one. Be more concise next time. In your case, you can write what you know and then what exactly in the parameter is the issue. I still want to know what's vague in the parameter.