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('http://192.168.18.2:1111/predict');
  // print(url);
  // Create a multipart request
  var request = http.MultipartRequest("POST", uri);
  // Create a multipart file
  var multipartFile = http.MultipartFile.fromBytes(
    'file',
    file,
    filename: filepath.split("/").last,
  );
  // Add the file to the request
  request.files.add(multipartFile);
  try{
    return await request.send();
  }
  catch(e){
    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 10.0.2.2 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