Flutter SocketException: Failed host lookup: 'xxxxxx.local' - flutter

I have a raspberry pi in my lan with the hostname 'xxx.local'. My phone is connected to the wifi but everytime I try to fetch data from my REST backend using the '.local' address i get this exception:
'SocketException (SocketException: Failed host lookup: 'xxx.local' (OS Error: No address associated with hostname, errno = 7))'
But it works fine when I try to connect to it via ssh or even when I try to fetch data using postman.
Is it possible that it has something to do with the mobile data, since it is deactivated because my test phone has no sim card inside. If yes, would it be possible to work without sim card?
The code where I try to fetch the data:
static Future<List<Device>> fetchDevices() async {
final response = await http.get('http://test-hub.local:9080/devices');
if (response.statusCode == 200) {
final data = json.decode(response.body);
print(data);
List<Device> responses =
data.map<Device>((j) => Device.fromJson(j)).toList();
print(responses);
return responses;
} else {
throw Exception(
'Failed to load Devices: ${response.statusCode} [${response.reasonPhrase}]');
}
}

I got stuck on this for a long time, The issue is that Android currently does not support mdns lookups (.local).
I don't know how you are getting your mdns address, but for people using the multicast_dns package you can fetch the ip address as described here. Then you can use the IP address for your http queries

Related

Appwrite sms authentication connection times out

I want to enable phone authentication on my flutter app with appwrite. I followed the official guidelines, changed the .env variables of appwrite and used docker compose up -d to restart appwrite with the correct credentials. As SMS provider I am using text-magic therefore my .env file has the following configurations for sms:
_APP_SMS_PROVIDER=sms://[USERNAME]:[API-KEY]#text-magic
_APP_SMS_FROM=+123456789
Username and API-Key come from textmagic
Additionallly I've created a simple method to create a phone Session.
createPhoneSession(String phonenumber) async {
try {
Client client = Client();
client
.setEndpoint(AppConstants.endpointId)
.setProject(AppConstants.projectId);
Account account = Account(client);
var token = await account.createPhoneSession(userId: ID.unique(), phone: phonenumber);
print(token.$id);
} catch (error) {
print(error);
}
}
The exception I get is the following:
I/flutter ( 5195): AppwriteException: null, Connection timed out (0)
Any suggestions why it keeps timing out? Thank you for your help in advance!
As you mentioned, the endpoint was incorrect. When developing locally, you can't use localhost for the endpoint because the mobile device will try to connect to itself rather than Appwrite. Instead of localhost, you can use your LAN IP.

How to connect wss in flutter web?

I try to connect a socket-io with flutter. Before, our server-side developer give me an URL without SSL-Certificate and everything is worked. But now, our server has SSL-Certificate, I can't connect to that socket-io. This is my code to connect:
Socket socket = io(
'wss://server-address',
OptionBuilder()
.setTransports(['websocket'])
.disableAutoConnect()
.build());
socket.connect();
socket.onConnect((_) {
print('socket connect');
});
socket.onConnectError((data) => print('socket error = ' + data.toString()));
I get this error:
socket error = {msg: websocket error, desc: null, type: TransportError}
I try to deploy my web application on a secure host like firebase but still have problems. In inspect of firefox, I also see this error:
Firefox cant establish a connection to the server
How to fix this problem? How to connect to secure socket-io address in flutter web?

SocketException (SocketException: No route to host (OS Error: No route to host, errno = 113)

I have a Flask application running on my local machine, which I'm using as a backend for the Flutter application , but I'm getting this error
SocketException (SocketException: No route to host (OS Error: No route to host, Errno = 113)
Future pix(String BackgroundImagePath, String originalImagePath) async {
final uri = Uri.parse('http://my desktop address:5000').replace(queryParameters: {
'original': originalImagePath,
'background': BackgroundImagePath,
});
final response = await http.get(uri);
The phone is connected to my laptop through a USB
Both phone & Laptop are on same WIFI
I replaced localhost with my Ubuntu desktop IP address
Try adding this line in your AndroidManifest.xml, inside the <manifest> tag.
I had the same problem and this solved it.
<uses-permission android:name="android.permission.INTERNET"/>

flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: SocketException: OS Error: Connection refused, errno =111,address =server ip, port = 33450

I have app with flutter . I have to connect to server my ip is like =https://91.#.#.#:5000/api
when i post data to server i get flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: SocketException: OS Error: Connection refused, errno =111,address =server ip, port = 33450
my code
var url = Uri.parse(AddressController.text);
http.Response response = await http.post(url, body: {});
SocketException usually occurs when you don't have internet please make user that you have internet, if you're testing in real device make sure you put internet permission in manifest

Flutter - How to Implement Network Service Discovery

I want to discover the deices which are running a specified service exposed to the local network, but I don't know how to.
For example I want local IP address and port number of the devices running service '_googlecast._tcp.'.
Is there any way to achieve this in Flutter ?
Thanks in advance
Check multicast DNS package: A Dart package to do service discovery over multicast DNS (mDNS), Bonjour, and Avahi.
Essentially, create a mDNS Client and get PTR record for the service, as:
const String name = '_googlecast._tcp.local';
final MDnsClient client = MDnsClient();
await client.start();
// Get the PTR recod for the service.
await for (PtrResourceRecord ptr in client
.lookup<PtrResourceRecord>(ResourceRecordQuery.serverPointer(name))) {
// Use the domainName from the PTR record to get the SRV record,
// which will have the port and local hostname.
// Note that duplicate messages may come through, especially if any
// other mDNS queries are running elsewhere on the machine.
await for (SrvResourceRecord srv in client.lookup<SrvResourceRecord>(
ResourceRecordQuery.service(ptr.domainName))) {
// Domain name will be something like "io.flutter.example#some-iphone.local._dartobservatory._tcp.local"
final String bundleId =
ptr.domainName; //.substring(0, ptr.domainName.indexOf('#'));
print('Dart observatory instance found at '
'${srv.target}:${srv.port} for "$bundleId".');
}
}
client.stop();
print('Done.');