leodriesch
leodriesch

Reputation: 5780

Future returning null in combination with FutureBuilder in Flutter

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'];
    });
  }

Upvotes: 0

Views: 5992

Answers (2)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657068

This code is a bit weird.

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

async allows you to avoid then (in most cases).

The code should rather be:

Future<String> _getImages(String url) async {
  var result = await http.get(url);
  // for debugging only
  print('statusCode: ${result.statusCode}');
  var contentType = result.headers['content-type'];
  print('content-type: $contentType');
  return contentType;
}

This way you can also check if the request actually provides a result.

Upvotes: 5

R&#233;mi Rousselet
R&#233;mi Rousselet

Reputation: 276891

Your _getImages function returns nothing. You need to return the result of your call.

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

Upvotes: 2

Related Questions