0

When I run my Flutter app on my Android phone and print the IP, sometimes it's showing an IP from my home network (192.168.x.x) but other times it's showing a different network (10.x.x.x). How to make it consistently use my home network?

Using the https://pub.dev/packages/wifi package:

Future<Null> _discoverPrintersWifi() async {
  final String ip = await Wifi.ip;

  print(ip);
}
Jon
  • 451
  • 1
  • 7
  • 19
  • You're probably selecting a different network interface. Can you post the code you use to print your IP? – salezica Apr 15 '21 at 19:43
  • I don't see anything in my code that chooses an interface. I updated the question with my code. – Jon Apr 15 '21 at 19:56
  • I don't see anything in that plugin's documentation that would allow you to select the network interface for the IP you're getting. You may need another approach if you need to look through the available interfaces and pick one – salezica Apr 15 '21 at 20:11
  • If one of the answers helped you, please consider upvoting – L. Gangemi Jul 09 '21 at 12:23

2 Answers2

0

you can make it consistent by assigning static IP to the phone.

however you should know how DHCP distribute the IPs .. meaning you will get a constant IP but at some point you might have no connection because somebody else is using same IP provided to him by DHCP.

Moaid ALRazhy
  • 1,604
  • 1
  • 3
  • 13
0

First of all I would check if the user is connected to the Wifi.

I suggest you to switch from the wifi package to wifi_info_flutter.

You can get the current IP in this way:

import 'package:wifi_info_flutter/wifi_info_flutter.dart';

var wifiIP = await WifiInfo().getWifiIP();

Here you can check a similar question: How to get device IP in Dart/Flutter

L. Gangemi
  • 3,110
  • 1
  • 22
  • 47