How to get device IP in Dart/Flutter

前端 未结 8 817
孤城傲影
孤城傲影 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:53

    You can use the wifi package for getting the local IP Address (for eg. 192.168.x.x). (as The NetworkInterface.list (from dart:io) is no longer supporting Android from 7.0 and above).

    Use the wifi package :

    import 'package:wifi/wifi.dart';
    

    You can retrieve the IP Address like this:

    Future getIp() async {
      String ip = await Wifi.ip;
      return ip;
    }
    

    You can display it on a Text widget using FutureBuilder:

    FutureBuilder(
          future: getIp(),
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting) {
              return Center(child: CircularProgressIndicator());
            } else {
              if (snapshot.hasError)
                return Center(child: Text('Error: ${snapshot.error}'));
              else
                return Center(child: Text('IP Address is : ${snapshot.data}')); //IP Address
            }
          },
        );
    

提交回复
热议问题