如何在整个应用程序中仅一次执行连续的Internet连接检查,我已经完成了几乎完整的应用程序,现在我需要添加Internet连接检查,请帮助,在此先感谢
@override
Widget build(BuildContext context) {
return StreamBuilder<ConnectivityResult>(
stream: connectivityStream,
builder: (context, snapshot) {
if (snapshot.hasData) {
final connectivityResult = snapshot.data;
if (connectivityResult == ConnectivityResult.none) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: NoInternetConnectionScreen(),
);
}
return MaterialApp(
debugShowCheckedModeBanner: false,
home: SplashScreen(),
routes: routes,
);
} else if (snapshot.hasError) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: NoInternetConnectionScreen(),
);
}
return Center(child: CircularProgressIndicator());
}
);
}
最佳答案
连接插件在其文档中指出,仅在存在网络连接时提供信息,而在网络连接到Internet时不提供信息
Note that on Android, this does not guarantee connection to Internet. For instance, the app might have wifi access but it might be a VPN or a hotel WiFi with no access.
您可以使用
import 'dart:io';
...
try {
final result = await InternetAddress.lookup('google.com');
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
print('connected');
}
} on SocketException catch (_) {
print('not connected');
}
关于flutter - 互联网连接困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63735909/