Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get local IP in Flutter Web

I'm trying to get the local IP address in a Flutter Web app. Searching the internet I came around this package: get_ip 0.4.0 - it states it is working under web.

I have this function:

Future<void> initPlatformState() async {
    print("Test");
    String ipAddress;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      ipAddress = await GetIp.ipAddress;
    } on PlatformException {
      ipAddress = 'Failed to get ipAddress.';
    } on Exception {
      print("Exception");
    }

    print("Ip: $ipAddress");
  }

and I call it in initState of main.dart.

The console just has the output Test, it does not output the IP or Exception.

Has someone already used this package? Would there be an other way for a web app to get the local IP?

(I'm using MacOS)

like image 384
helmut1978 Avatar asked Oct 14 '25 04:10

helmut1978


2 Answers

The get_ip package states that it supports Android/iOS. So,instead of using the package,you can directly interact with ipify endpoint. Check out their API documentation for more!

Future<String> getIP() async {
  try {
    const url = 'https://api.ipify.org';
    var response = await http.get(url);
    if (response.statusCode == 200) { 
      print(response.body);
      return response.body;
    } else {
      print(response.body);
      return null;
    }
  } catch (exception) {
    print(exception);
    return null;
  }
}
like image 188
vasanth kumar Avatar answered Oct 16 '25 21:10

vasanth kumar


I'm Using ip_geolocation_api Package for getting an IP address and it is working as expected

  String text = '';
  GeolocationData geolocationData;

  @override
  void initState() {
    super.initState();
    this.getIp();
  }

  Future<void> getIp() async {
    geolocationData = await GeolocationAPI.getData();
    if (geolocationData != null) {
      setState(() {
        text = geolocationData.ip;
        print(text);
      });
    }
  }

Now You have IP in the text Variable.

like image 30
Shailandra Rajput Avatar answered Oct 16 '25 21:10

Shailandra Rajput