如何在Android上获取IP地址?
我正在尝试获取IP地址以显示,但我的IP地址却无效。 当我模拟它时,这也有效,但是当我在Android设备上尝试它时,这也不行。 这里有什么问题? 任何帮助将不胜感激! 这是我的代码:
[import 'package:bottom_navigation_test/sidemenu.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as https;
import 'widget/info_widget.dart';
class MyIP extends StatefulWidget {
@override
_MyIP createState() => _MyIP();
}
class _MyIP extends State<MyIP> {
Map<String, dynamic> map = {};
@override
void initState() {
super.initState();
init();
}
Future init() async {
final ipAddress = await MyIPInfo.getIPAddress();
if (!mounted) return;
setState(() => map = {
'IP Address:': ipAddress,
});
}
Widget build(BuildContext context) => Scaffold(
drawer: SideMenu(),
appBar: AppBar(
backgroundColor: const Color.fromARGB(255, 89, 137, 199),
title: const Text('My IP'),
),
body: InfoWidget(map: map),
);
}
class MyIPInfo {
static Future<String?> getIPAddress() async {
try {
final url = Uri.parse('https://api.ipify.org');
final response = await https.get(url);
return response.statusCode == 200 ? response.body : null;
} catch (e) {
return null;
}
}
}][1]
I am trying to get the IP Address to show but I am getting null.
Also this works when I simulate it, but not when I am trying it on an android device.
What could be the issue here?
Any help would be appreciated!
Here is my code:
[import 'package:bottom_navigation_test/sidemenu.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as https;
import 'widget/info_widget.dart';
class MyIP extends StatefulWidget {
@override
_MyIP createState() => _MyIP();
}
class _MyIP extends State<MyIP> {
Map<String, dynamic> map = {};
@override
void initState() {
super.initState();
init();
}
Future init() async {
final ipAddress = await MyIPInfo.getIPAddress();
if (!mounted) return;
setState(() => map = {
'IP Address:': ipAddress,
});
}
Widget build(BuildContext context) => Scaffold(
drawer: SideMenu(),
appBar: AppBar(
backgroundColor: const Color.fromARGB(255, 89, 137, 199),
title: const Text('My IP'),
),
body: InfoWidget(map: map),
);
}
class MyIPInfo {
static Future<String?> getIPAddress() async {
try {
final url = Uri.parse('https://api.ipify.org');
final response = await https.get(url);
return response.statusCode == 200 ? response.body : null;
} catch (e) {
return null;
}
}
}][1]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论