dart 获取手机信息,如何在Dart / Flutter中获得设备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?

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.

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.

解决方案

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

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

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

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 {

String _networkInterface;

@override

initState() {

super.initState();

NetworkInterface.list(includeLoopback: false, type: InternetAddressType.any)

.then((List 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"),

),

);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值