How to get device IP in Dart/Flutter

前端 未结 8 853
孤城傲影
孤城傲影 2021-02-13 21:01

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

8条回答
  •  佛祖请我去吃肉
    2021-02-13 21:48

    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 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.

提交回复
热议问题