如何在颤动网中获取本地IP [英] How to get local IP in Flutter Web

查看:15
本文介绍了如何在颤动网中获取本地IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Ffltter Web应用程序中获取本地IP地址。在互联网上搜索,我找到了这个包:get_ip0.4.0-它说明它在Web下工作。

我有这个功能:

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");
  }

我在main.dartinitState中调用它。

控制台只有输出Test,不输出IP或Exception

是否已有人使用此程序包?Web应用还会有其他方式获取本端IP吗?

(我使用的是MacOS)

推荐答案

get_ip套餐说明支持android/iOS。 因此,您可以直接与ipify端点交互,而不是使用该包。 有关更多信息,请查看他们的API文档!

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;
  }
}

这篇关于如何在颤动网中获取本地IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆