如何在Dart / Flutter中获得设备IP [英] How to get device IP in Dart/Flutter

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

问题描述

我当前正在编写一个应用程序,用户需要在其中知道其手机/平板电脑的IP地址。我在哪里可以找到此信息?

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?

我只想知道本地IP地址是什么,例如192.168.x.xxx,而不是本地IP地址。路由器。

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.

到目前为止,我似乎只能找到 InternetAddress.anyIPv4 InternetAddress .loopbackIPv4 。环回地址不是我想要的,因为它是127.0.0.1。

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.

推荐答案

我猜你的意思是当前的本地IP

I guess you mean the local IP of the currently connected Wifi network, right?

检查 dart:io中的NetworkInterface

编辑:Android不支持NetworkInterface.list,稍后由 Mahesh 指出。除了 wifi包 / 53139132/796963>他的答案,有一个 PR 可以带来

NetworkInterface.list is not supported in Android, as later pointed out by Mahesh. Aside from the wifi package suggested in his answer, there's a PR to bring that to the connectivity plugin.

您可能还想使用 flutter / plugins 中的/ packages / connectivity rel = noreferrer> connectivity 插件。

You may also want to check if Wifi is available using the connectivity plugin in flutter/plugins.

下面有一个使用NetworkInferface的简单示例。顺便说一句,这是如何检查wifi是否可用的示例

Right below there's a simple example of usage of NetworkInferface. By the way, here's an example of how to check if wifi is available.

import 'dart:io';
import 'package:flutter/material.dart';

main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: "Network Interface example",
      home: new NetworkInterfaceWidget(),
    );
  }
}

class NetworkInterfaceWidget extends StatefulWidget {
  @override
  _NetworkInterfaceState createState() => new _NetworkInterfaceState();
}

class _NetworkInterfaceState extends State<NetworkInterfaceWidget> {
  String _networkInterface;
  @override
  initState() {
    super.initState();

    NetworkInterface.list(includeLoopback: false, type: InternetAddressType.any)
    .then((List<NetworkInterface> interfaces) {
      setState( () {
        _networkInterface = "";
        interfaces.forEach((interface) {
          _networkInterface += "### name: ${interface.name}\n";
          int i = 0;
          interface.addresses.forEach((address) {
            _networkInterface += "${i++}) ${address.address}\n";
          });
        });
      });
    });
  }

  @override
  Widget build(BuildContext context) {    
    return Scaffold(
      appBar: AppBar(
        title: Text("NetworkInterface"),
      ),
      body: Container(
        padding: EdgeInsets.all(10.0),
        child: Text("Only in iOS.. :(\n\nNetworkInterface:\n $_networkInterface"),
      ),
    );
  }
}

这篇关于如何在Dart / Flutter中获得设备IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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