2

What is the purpose of initialData in FutureBuilder? The documentation says:-

The data that will be used to create the snapshots provided until a non-null future has completed

Does that mean the placeholder data that gets shown to the user when the data is awaiting?

If that was the case then why my FutureBuilder always shows the loading state and not the initialData

My FutureBuilder()


FutureBuilder(
      future: _getPosts(),
      initialData: _getOfflineData(),
      builder: (BuildContext context, AsyncSnapshot<List<Post>> snapshot) {
        if (!snapshot.hasData ||
            snapshot.data == null ||
            snapshot.data.isEmpty ||
            snapshot.hasError) {
          // Still loading || load null data UI
          return ListView(
            scrollDirection: Axis.horizontal,
            children: [for (var i = 0; i < 5; i++) HorizontalPostShimmer()],
          );
        }
        return ListView.builder(
          itemCount: snapshot.data.length,
          scrollDirection: Axis.horizontal,
          itemBuilder: (context, index) {
            final titleExists = doesTitleExist(snapshot.data[index].title);
            final title = titleExists
                ? snapshot.data[index].title
                : _translate.translate('unknownTitle');
            final imgExists =
                doesImageExist(snapshot.data[index].featuredMedia);
            final img = imgExists ? snapshot.data[index].featuredMedia : null;
            return Container(
              width: 300.0,
              child: Card(
                child: InkWell(
                  onTap: () {
                    Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) =>
                                SAPost(data: snapshot.data[index]),
                            settings: RouteSettings(name: 'SAPost')));
                  },
                  child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Row(
                      children: [
                        img == null
                            ? Container(
                                child: SaviorImageShimmer(
                                height: 100,
                                width: 100,
                              ))
                            : CachedNetworkImage(
                                imageUrl: img,
                                placeholder: (context, url) =>
                                    SaviorImageShimmer(),
                                errorWidget: (context, url, error) =>
                                    Icon(Icons.error),
                              ),
                        Expanded(
                          child: Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: Text(
                              '$title',
                              style: TextStyle(
                                fontSize: _isMobile ? 18.0 : 22.0,
                              ),
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                ),
              ),
            );
          },
        );
      },
    )

And my _getPosts()


Future<List<Post>> _getPosts() {
    final Map<String, dynamic> args = {
      'IDs': [1983],
      'perPage': 10,
      'pageNum': 1,
      'from': 'horizontalSlider'
    };

    final Future<List<Post>> posts =
        context.read(fetchSpecificPostsByCategoriesProvider(args).future);
    return posts;
  }

And my _getOfflineData()


List<Post> _getOfflineData() {
    final List<Post> cachedPosts =
        Hive.box('posts').get('horizontalSlider', defaultValue: <Post>[]);
    return cachedPosts;
  }


Am I doing something that my FutureBuilder() always returns the ListView about loading? Or is initialData used in another way

2 Answers2

0

In case if you would like to provide some data while other data is being fetched, you can use the initialData property to provide that data.

Preet Shah
  • 792
  • 5
  • 12
0

The documentation is as clear as it can be. My guess is your initial data is empty. Try removing the following condition from the build function and check again: snapshot.data.isEmpty.

Riwen
  • 4,734
  • 2
  • 19
  • 31