Home > Net >  body might complete normally
body might complete normally

Time:11-07

in past I have many times faced off with similar null safety problems but this time I cannot fix it. Vscode says:

The body might complete normally, causing 'null' to be returned

but the return type is a potentially non-nullable type. for ItemBuilder (ListviewBuilder):

@override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      backgroundColor: Colors.grey[200],
      body: FutureBuilder<AdBanner>(
        future: _futureAd,
        builder: (BuildContext context, AsyncSnapshot<AdBanner> snapshot) {
          if (snapshot.hasData) {
            final advertisements = snapshot.data!.data;

            return ListView.builder(
                itemCount: advertisements!.length,
                itemBuilder: (BuildContext context, int index) {
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => CategoryViewDetail(
                                jsondata: advertisements,
                                tabbarenable: true,
                              )));
                });
            

            // BannerListTile(advertisements, index, context));
          } else if (snapshot.hasError) {
            return NewsError(
              errorMessage: '${snapshot.hasError}',
            );
          } else {
            return const NewsLoading(
              text: 'Loading...',
            );
          }
        },
      ),
    );
  }
}

CodePudding user response:

You're simply missing a return inside your ListView.builder. It should be something like:

ListView.builder(
   itemCount: advertisements!.length,
   itemBuilder: (BuildContext context, int index) {
      // Fix your previous code here with your widget. 
       return Text("Something"):
});

CodePudding user response:

Try use like this:

 @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      backgroundColor: Colors.grey[200],
      body: FutureBuilder<AdBanner>(
        future: _futureAd,
        builder: (BuildContext context, AsyncSnapshot<AdBanner> snapshot) {
          if (snapshot.hasData) {
            final advertisements = snapshot.data!.data;

            return ListView.builder(
                itemCount: advertisements!.length,
                itemBuilder: (BuildContext context, int index) {
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => CategoryViewDetail(
                                jsondata: advertisements,
                                tabbarenable: true,
                              )));
                });


            // BannerListTile(advertisements, index, context));
          } 
           if (snapshot.hasError) {
            return NewsError(
              errorMessage: '${snapshot.hasError}',
            );
          } 
            return const NewsLoading(
              text: 'Loading...',
            );
          
        },
      ),
    );
  }
}

If this doesn't work try adding a null check on your future builder

FutureBuilder<AdBanner>?
  • Related