Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter flatter:在Android上获取本地IP地址_Flutter_Dart_Network Programming_Ip Address - Fatal编程技术网

Flutter flatter:在Android上获取本地IP地址

Flutter flatter:在Android上获取本地IP地址,flutter,dart,network-programming,ip-address,Flutter,Dart,Network Programming,Ip Address,如何在Flatter中获取我的(Android)设备的本地IP地址? 这应该是 连接到WIFI时,路由器通过DHCP分配的本地IP地址 如果连接到VPN,则由我的VPN服务器分配的VPN网络中的本地IP地址(而不是VPN服务器本身分配的全局IP地址) 通过蜂窝网络连接时的全局IP 我现在是这样解决的,但如果你有更好的解决方案,那就太好了: static Future<String> getLocalIpAddress() async { final interfaces =

如何在Flatter中获取我的(Android)设备的本地IP地址? 这应该是

  • 连接到WIFI时,路由器通过DHCP分配的本地IP地址
  • 如果连接到VPN,则由我的VPN服务器分配的VPN网络中的本地IP地址(而不是VPN服务器本身分配的全局IP地址)
  • 通过蜂窝网络连接时的全局IP

我现在是这样解决的,但如果你有更好的解决方案,那就太好了:

static Future<String> getLocalIpAddress() async {
    final interfaces = await NetworkInterface.list(type: InternetAddressType.IPv4, includeLinkLocal: true);

    try {
      // Try VPN connection first
      NetworkInterface vpnInterface = interfaces.firstWhere((element) => element.name == "tun0");
      return vpnInterface.addresses.first.address;
    } on StateError {
      // Try wlan connection next
      try {
        NetworkInterface interface = interfaces.firstWhere((element) => element.name == "wlan0");
        return interface.addresses.first.address;
      } catch (ex) {
        // Try any other connection next
        try {
          NetworkInterface interface = interfaces.firstWhere((element) => !(element.name == "tun0" || element.name == "wlan0"));
          return interface.addresses.first.address;
        } catch (ex) {
          return null;
        }
      }
    }
  }
静态未来getLocalIpAddress()异步{
final interfaces=wait NetworkInterface.list(类型:InternetAddressType.IPv4,includeLinkLocal:true);
试一试{
//请先尝试VPN连接
NetworkInterface vpnInterface=interfaces.firstWhere((元素)=>element.name==“tun0”);
返回vpnInterface.addresses.first.address;
}论状态误差{
//下一步尝试wlan连接
试一试{
NetworkInterface interface=interfaces.firstWhere((element)=>element.name==“wlan0”);
返回interface.addresses.first.address;
}捕获(ex){
//下一步尝试其他连接
试一试{
NetworkInterface interface=interfaces.firstWhere((element)=>!(element.name==“tun0”| | element.name==“wlan0”);
返回interface.addresses.first.address;
}捕获(ex){
返回null;
}
}
}
}
看看这些插件: