Search code examples
httpdartflutterhttp-status-codes

Flutter - Getting HTTP Response from url_launcher


In Flutter it's easy to take the user to a website using url_launcher (FIG A). I'm looking for a way to return a HTTP status code upon completion / failure. This would ideally be returned in a string for text output.

FIG A;

_launchURL() async {
  const url = 'https://google.com/';
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}

Thanks


Solution

  • import 'package:http/http.dart' as http;
    
    void _loadFromUrl(String url) async {
      http.Response response = await http.get(url);
      if (response.statusCode == 200) {...}
      else {...}
    

    I hope it'll help