connection failed while calling api in flutter


I am connecting api using swagger it is giving excelent result but when hitting api via mobile app it throws error
connection failed: OS Error: Network is unreachable errorno= 101

my api calling code

  var file = await File(filepath).readAsBytes();
  var uri = Uri.parse('');
  // print(url);
  // Create a multipart request
  var request = http.MultipartRequest("POST", uri);
  // Create a multipart file
  var multipartFile = http.MultipartFile.fromBytes(
    filename: filepath.split("/").last,
  // Add the file to the request
    return await request.send();
    return http.StreamedResponse(Stream.error(e), 404);

>Solution :

This could be due to a number of issues.

  • Make sure your device is connected,
  • Wifi or data is enabled,
  • your app has permissions,

and if you’re using HTTP instead of HTTPS, on android and iOS you must allow clear text on Android 8 or higher.

Best place to start is to look at logcat while executing the code which will give an idea what is causing the failure. If you’re connecting to local machine from an emulator, you also need to use the address instead.

If you’re connecting on a local network from a real device to a PC, make sure you’re in the same network, firewalls allow your connection .etc. as well.

Leave a Reply Cancel reply