dartipflutter

How to get device IP in Dart/Flutter


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.


Solution

  • 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 all Android devices (as pointed out by Mahesh). The wifi package provides that, but later this was incorporated into the flutter's connectivity plugin. In Oct/2020 the methods for that were moved from the connectivity to the wifi_info_flutter plugin, and in 2021 that package was discontinued in favor of network_info_plus.

    So just go for network_info_plus and call await NetworkInfo().getWifiIP().


    By the way, you may also want to check if Wifi is available using the connectivity_plus plugin in flutter/plugins. Here's an example of how to check if wifi is available.

    FLUTTER WEB

    Such functionality is incompatible with flutter web, as it runs in a browser. However, you can use a server side helper to get that.