Future returning null also using FutureBuilder in Flutter

3,399 views
Skip to first unread message

leo nx

unread,
Jun 11, 2018, 12:33:04 PM6/11/18
to Flutter Dev
I want to use a FutureBuilder to check if a url is a png image, and then build either one or two images(in a list). But somehow the Future always returns null when i print it...

The result is the app always building the listview with the two CachedNetworkImages, which is not what i want it to do. If the URL is an image, it should just build a CachedNetworkImage with that url, and if not it should alter the url and build a listview with 2 images.

    child: new FutureBuilder(
                    future
: _getImages(widget.imgUrl),
                    builder
: (BuildContext context, AsyncSnapshot snapshot) {
                     
switch (snapshot.connectionState) {
                       
case ConnectionState.none:
                         
return new Text('Press button to start');
                       
case ConnectionState.waiting:
                         
return new Text('Awaiting result...');
                       
default:
                         
if (snapshot.hasError)
                           
return new Text('Error: ${snapshot.error}');
                         
else {
                           
print(snapshot.data);
                           
if (snapshot.data == "image/png") {
                             
return new SingleChildScrollView(
                                child
: new CachedNetworkImage(
                                  imageUrl
: widget.imgUrl,
                                  placeholder
: new Center(
                                      child
: new AdaptiveProgressIndicator()),
                               
),
                             
);
                           
} else {
                             
return new ListView(
                                children
: <Widget>[
                                 
new CachedNetworkImage(
                                    imageUrl
:
                                        widget
.imgUrl.split('.png')[0] + '-0.png',
                                    placeholder
: new Center(
                                        child
: new AdaptiveProgressIndicator()),
                                 
),
                                 
new CachedNetworkImage(
                                    imageUrl
:
                                        widget
.imgUrl.split('.png')[0] + '-1.png',
                                 
)
                               
],
                             
);
                           
}
                         
}
                     
}
                   
}),
             
),
           
));
     
}
   
     
Future<String> _getImages(String url) async {
        await http
.get(url).then((result) {
         
return result.headers['content-type'];
       
});
     
}

Jonah Williams

unread,
Jun 11, 2018, 12:54:40 PM6/11/18
to leo nx, Flutter Dev
It looks like you aren't returning the Future from _getImages.  The return type is still a future because you are calling await, but if you want to use the value in that future you need to return it explicitly.


Future<String> _getImages(String url) async {
  final result = await http.get(url);
  return result.headers['content-type'];
}

Or

Future<String> _getImages(String url)  {
  return http.get(url).then((result) {
    return result.headers['content-type'];
  });
}


--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Allan Jeremy

unread,
Jun 11, 2018, 2:22:40 PM6/11/18
to Flutter Dev
This is a really good answer, the internet needs more people like you, cheers
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages