Flutter:检查互联网连接并根据输出导航?
Posted
技术标签:
【中文标题】Flutter:检查互联网连接并根据输出导航?【英文标题】:Flutter: Check Internet connectivity and navigate based on output? 【发布时间】:2020-05-19 23:22:52 【问题描述】:我是flutter的新手,我想检查互联网是否可用,根据情况,屏幕必须改变。我已经编写了下面的代码(屏幕开关工作正常),但我无法获得布尔输出(互联网)。当我在检查互联网类中删除 Future 时,它会引发错误。你能解决这个问题吗:
class _ScreenState extends State<ChannelScreen>
bool isInternet;
bool result;
@override
void initState()
// TODO: implement initState
result = check();
super.initState();
@override
Widget _buildChild()
print ("The output “);
print (result);
if (result != Null && result == true)
// if internet is ON
return Container();
//if internet is off
return Container();
Widget build(BuildContext context)
return new Container(child: _buildChild());
Future<bool> check() async
var connectivityResult = await Connectivity().checkConnectivity();
if (connectivityResult == ConnectivityResult.mobile)
print ("******* Mobile is ON ******");
return true;
else if (connectivityResult == ConnectivityResult.wifi)
print ("******* Wifi is ON ******");
return true;
print ("No connectivity");
return false;
【问题讨论】:
您必须在 _buildChild 方法中使用 FutureBuilder。 api.flutter.dev/flutter/widgets/FutureBuilder-class.html 【参考方案1】:您可以使用StreamBuilder
StreamBuilder(
stream: Connectivity().onConnectivityChanged,
builder: (context, snapshot)
// Use this to avoid null exception
if (snapshot.connectionState == ConnectionState.none)
return CircularProgressIndicator();
else
ConnectivityResult result = snapshot.data;
// Check Connectivity result here and display your widgets
if(ConnectivityResult.none)
yourWidgetForNoInternet();
else
yourWidgetForInternet();
,
)
【讨论】:
【参考方案2】:bool hasInternet = false, isChecking = true;
@override
void initState()
super.initState();
check();
@override
Widget build(BuildContext context)
return Container(
child: isChecking
? ListTile(
leading: CircularProgressIndicator(), title: Text('Checking...'))
: hasInternet
? ListTile(title: Text('Your widget here...'))
: ListTile(
leading: Icon(Icons.info),
title: Text('No Internet Conncetion')));
check() async
var connectivityResult = await Connectivity().checkConnectivity();
if (connectivityResult == ConnectivityResult.mobile)
print("******* Mobile is ON ******");
setState(()
isChecking = false;
hasInternet = true;
//navigate to another screen.....
);
else if (connectivityResult == ConnectivityResult.wifi)
print("******* Wifi is ON ******");
setState(()
isChecking = false;
hasInternet = true;
//navigate to another screen.....
);
else
setState(()
isChecking = false;
hasInternet = false;
);
【讨论】:
【参考方案3】:有一个插件可以使用连接:
https://pub.dev/packages/connectivity#-readme-tab-
我使用以下代码检查互联网是否已连接
static Future<bool> checkInternetConnectivity() async
bool isConnected;
try
final result = await InternetAddress.lookup('google.com');
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty)
isConnected = true;
on SocketException catch (_)
isConnected = false;
return isConnected;
【讨论】:
以上是关于Flutter:检查互联网连接并根据输出导航?的主要内容,如果未能解决你的问题,请参考以下文章
你如何在flutter.dart中连续检查互联网访问,而不是连接[重复]