I tried to call my api with flutter but when i make a request i have this error:
E/flutter (15338): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Connection refused
E/flutter (15338): #0 IOClient.send (package:http/src/io_client.dart:88:7)
E/flutter (15338): E/flutter (15338): #1 BaseClient._sendUnstreamed (package:http/src/base_client.dart:93:32)
E/flutter (15338): E/flutter (15338): #2_withClient (package:http/http.dart:164:12)
E/flutter (15338): < asynchronous suspension>
Have you ever seen this error?
CodePudding user response:
I bet you are hosting the nodejs api locally at the moment? Also I bet youre testing on android? You are receiving this error because you cant directly connect to a localhost via android emulator/device. So you basically need to check which device is currently used. and change the localhost with your network port. for me its 192.168.178.23 for example.
put this in your code:
import 'dart:io';
String url = Platform.isAndroid ? 'http://192.168.178.23:3006' : 'http://localhost:3006';
and then you can change your request like this:
final response = await dio.get('${url}/api/myapi/myendpoint');
Hope it helps.
CodePudding user response:
Check whether your node js server is running and check your url and domain name.