I want to connect my flutter application to my API but got this error

58 Views Asked by At

`

 Future<void> fetchData() async {
        final String apiUrl = 'http://192.168.100.90:4002/booking/getall';
        try {
          // Making GET request
          final response = await http.get(Uri.parse(apiUrl));
          print(response.statusCode);
          // Decoding response
          if (response.statusCode == 200) {
            // If the server returns a 200 OK response,
            // parse the JSON
            final Map<String, dynamic> responseData = json.decode(response.body);
    
            // Process the data here if needed
            print(responseData);
          } else {
            // If the server did not return a 200 OK response,
            // throw an exception
            throw Exception('Failed to load data');
          }
        } catch (e) {
          // Error handling
          print('Error: $e');
          throw e;
        }
      }

`

I/flutter ( 2707): Error: ClientException with SocketException: Connection timed out (OS Error: Connection timed out, errno = 110), address = 192.168.100.90, port = 48130, uri=http://192.168.100.90:4002/booking/getall
E/flutter ( 2707): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: ClientException with SocketException: Connection timed out (OS Error: Connection timed out, errno = 110), address = 192.168.100.90, port = 48130, uri=http://192.168.100.90:4002/booking/getall

I am trying to connect my API to my flutter application and I am finding a solution of this error. what should i do now

1

There are 1 best solutions below

0
Abdul Awal On

This error occurs because the port emulator can read/write(CRUD) and your server's actual port where the server is running on (For local dev machine only) is not the same.

Change the URL on your Flutter project code to this.


 uri=http://192.168.100.90:4002/booking/getall

To


  uri = 'http://10.0.2.2:5500/booking/getall';
     http://10.0.2.2:PortNumberFromError


And make sure you have provided the internet permission on the manifest

<manifest xlmns:android...>
 ...
 <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 <application ...
</manifest>