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 IP address is, such as, 192.168.x.xxx and NOT the public IP address of the router.
So far, I can only seem to find InternetAddress.anyIPv4
and InternetAddress.loopbackIPv4
. The loopback address is not what I want as it is 127.0.0.1.
I guess you mean the local IP of the currently connected Wifi network, right?
Check NetworkInterface in 'dart:io'.
EDIT: NetworkInterface.list is not supported in Android, as later pointed out by Mahesh. Aside from the wifi package suggested in his answer, there's a PR to bring that to the connectivity plugin.
You may also want to check if Wifi is available using the connectivity plugin in flutter/plugins.
Right below there's a simple example of usage of NetworkInferface. By the way, here's an example of how to check if wifi is available.
import 'dart:io';
import 'package:flutter/material.dart';
main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: "Network Interface example",
home: new NetworkInterfaceWidget(),
);
}
}
class NetworkInterfaceWidget extends StatefulWidget {
@override
_NetworkInterfaceState createState() => new _NetworkInterfaceState();
}
class _NetworkInterfaceState extends State<NetworkInterfaceWidget> {
String _networkInterface;
@override
initState() {
super.initState();
NetworkInterface.list(includeLoopback: false, type: InternetAddressType.any)
.then((List<NetworkInterface> interfaces) {
setState( () {
_networkInterface = "";
interfaces.forEach((interface) {
_networkInterface += "### name: ${interface.name}\n";
int i = 0;
interface.addresses.forEach((address) {
_networkInterface += "${i++}) ${address.address}\n";
});
});
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("NetworkInterface"),
),
body: Container(
padding: EdgeInsets.all(10.0),
child: Text("Only in iOS.. :(\n\nNetworkInterface:\n $_networkInterface"),
),
);
}
}
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.
This should provide the information you asked for
import 'dart:io';
...
Future printIps() async {
for (var interface in await NetworkInterface.list()) {
print('== Interface: ${interface.name} ==');
for (var addr in interface.addresses) {
print(
'${addr.address} ${addr.host} ${addr.isLoopback} ${addr.rawAddress} ${addr.type.name}');
}
}
}
See also
https://api.dartlang.org/stable/2.0.0/dart-io/NetworkInterface-class.html
Have you tried the device_info package?
There is an example on querying device information in https://pub.dartlang.org/packages/device_info#-example-tab-