I am currently writing an app where the user needs to know the IP address of their phone/tablet. Where would I find this information?
I only want to know what the local
In my recent app I have a requirement to get user's Ip address and then I found this packgage useful. https://pub.dev/packages/get_ip
Here is How I use it.
_onLoginButtonPressed() async {
String ipAddress = await GetIp.ipAddress;
print(ipAddress); //192.168.232.2
}
I was searching for getting IP address in flutter for both the iOS and android platforms.
As answered by Feu and Günter Zöchbauer following works on only iOS platform
NetworkInterface.list(....);
this listing of network interfaces is not supported for android platform.
After too long search and struggling with possible solutions, for getting IP also on android device, I came across a flutter package called wifi, with this package we can get device IP address on both iOS and android platforms. Simple sample function to get device IP address
Future<InternetAddress> get selfIP async {
String ip = await Wifi.ip;
return InternetAddress(ip);
}
I have tested this on android using wifi and also from mobile network. And also tested on iOS device.
Though from name it looks only for wifi network, but it has also given me correct IP address on mobile data network [tested on 4G network].
#finally_this_works : I have almost given up searching for getting IP address on android and was thinking of implementing platform channel to fetch IP natively from java code for android platform [as interface list was working for iOS]. This wifi package saved the day and lots of headache.
Here is another way.
Future<InternetAddress> _retrieveIPAddress() async {
InternetAddress result;
int code = Random().nextInt(255);
var dgSocket = await RawDatagramSocket.bind(InternetAddress.anyIPv4, 0);
dgSocket.readEventsEnabled = true;
dgSocket.broadcastEnabled = true;
Future<InternetAddress> ret =
dgSocket.timeout(Duration(milliseconds: 100), onTimeout: (sink) {
sink.close();
}).expand<InternetAddress>((event) {
if (event == RawSocketEvent.read) {
Datagram dg = dgSocket.receive();
if (dg != null && dg.data.length == 1 && dg.data[0] == code) {
dgSocket.close();
return [dg.address];
}
}
return [];
}).firstWhere((InternetAddress a) => a != null);
dgSocket.send([code], InternetAddress("255.255.255.255"), dgSocket.port);
return ret;
}
I guess you mean the local IP of the currently connected Wifi network, right?
EDITED
In this answer, I used to suggest using the NetworkInterface in 'dart:io', however NetworkInterface.list
is not supported in Android (as pointed out by Mahesh). The wifi package provides that, but later this was incorporated to the flutter's connectivity
plugin. In Oct/2020 the methods for that were moved from the connectivity
to the wifi_info_flutter plugin.
So just go for wifi_info_flutter
and call await WifiFlutter().getWifiIP()
.
By the way, you may also want to check if Wifi is available using the connectivity plugin in flutter/plugins. Here's an example of how to check if wifi is available.
You can use the wifi package for getting the local IP Address (for eg. 192.168.x.x). (as The NetworkInterface.list (from dart:io) is no longer supporting Android from 7.0 and above).
Use the wifi package :
import 'package:wifi/wifi.dart';
You can retrieve the IP Address like this:
Future<String> getIp() async {
String ip = await Wifi.ip;
return ip;
}
You can display it on a Text widget using FutureBuilder:
FutureBuilder<String>(
future: getIp(),
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
} else {
if (snapshot.hasError)
return Center(child: Text('Error: ${snapshot.error}'));
else
return Center(child: Text('IP Address is : ${snapshot.data}')); //IP Address
}
},
);
It seems that Dart doesn't have a solution to get your own ip address. Searching for a solution I came across the rest api https://ipify.org to get my public address. Hope it helps.