Home > Back-end >  FutureBuilder block null issue
FutureBuilder block null issue

Time:10-02

error: The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type. Try adding either a return or a throw statement at the end.

I am getting this error for FutureBuilder line and I could not find how to fix it. The problem is I cannot get it the problem here.

Any help would be really appreciated

Widget build(BuildContext context) => Scaffold(
        body: FutureBuilder<Article>(
          future: _futureArticle,
          builder: (BuildContext context, AsyncSnapshot<Article> snapshot) {
            if (snapshot.hasData) {
              final article = snapshot.data?.data;
              CustomScrollView(
                slivers: [
                  SliverAppBar(
                    // backgroundColor: Colors.red,
                    expandedHeight: 200,
                    floating: true,
                    pinned: true,
                    flexibleSpace: FlexibleSpaceBar(
                      background: Image.network(article == null
                          ? 'https://www.tonymacx86.com/attachments/2019-02-05-07-50-43-jpeg.385401/'
                          : article.imageUrl!),
                      title: Text('${article!.title}'),
                      centerTitle: true,
                    ),
                    leading: const Icon(Icons.arrow_back),
                    actions: const [
                      Icon(Icons.settings),
                      SizedBox(width: 12),
                    ],
                  ),
                  buildImages(),
                ],
              );
            }
            
            
          },
          
        ),
      );

  Widget buildImages() => SliverToBoxAdapter(
        child: Card(
          elevation: 4.0,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(10.0),
          ),
          child: ListTile(
            // leading: Image.network(article.imageUrl),
            title: Text(
              'Title: ',
            ),
            subtitle: Text(
              'Content: ',
            ),
          ),
        ),
      );
}

CodePudding user response:

You must return a widget from the builder function.

Widget build(BuildContext context) => Scaffold(
        body: FutureBuilder<Article>(
          future: _futureArticle,
          builder: (BuildContext context, AsyncSnapshot<Article> snapshot) {
            if (snapshot.hasData) {
              final article = snapshot.data?.data;
           return   CustomScrollView(
                slivers: [
                  SliverAppBar(
                    // backgroundColor: Colors.red,
                    expandedHeight: 200,
                    floating: true,
                    pinned: true,
                    flexibleSpace: FlexibleSpaceBar(
                      background: Image.network(article == null
                          ? 'https://www.tonymacx86.com/attachments/2019-02-05-07-50-43-jpeg.385401/'
                          : article.imageUrl!),
                      title: Text('${article!.title}'),
                      centerTitle: true,
                    ),
                    leading: const Icon(Icons.arrow_back),
                    actions: const [
                      Icon(Icons.settings),
                      SizedBox(width: 12),
                    ],
                  ),
                  buildImages(),
                ],
              );
            }
            
            
          },
          
        ),
      );

And you must return something even if snapshot.hasData is false.

  • Related