如何全局检查互联网连接并显示无连接屏幕
How to check internet connection globally and show no connection screen
我在我的项目中使用 connectivity 包来检查互联网连接。
文件main.dart
代码:
StreamProvider<ConnectivityResult>(
create: (context) =>
InternetConnectionService().connectionStatusController.stream,
child: MaterialApp(
.....
并且在每个屏幕上我都这样检查互联网连接:
bool hasConnection;
void checkConnectivity(context) async {
var connectivityResult = Provider.of<ConnectivityResult>(context);
if (connectivityResult == ConnectivityResult.none ||
connectivityResult == null) {
setState(() {
hasConnection = false;
});
} else {
setState(() {
hasConnection = true;
});
}
}
Widget build(BuildContext context) {
checkConnectivity(context);
return hasConnection == true
? Scaffold()
: NoInternetScreen();
}
我如何在全局而不是在每个屏幕上从根或一个小部件上检查连接并且不显示连接屏幕?
在您的 MaterialApp 小部件中,有一个构建器。您可以使用构建器将路径包装在任何小部件中。尝试这样做:
MaterialApp(
...
builder: (context, child) {
return StreamBuilder<ConnectivityResult>(
stream: InternetConnectionService().connectionStatusController.stream,
builder: (context, snapshot) {
final conenctivityResult = snapshot.data;
if (connectivityResult == ConnectivityResult.none || connectivityResult == null) return NoInternetScreen();
return child;
}
);
}
);
现在您不必在其他文件中添加任何互联网逻辑。您可以简单地编写排除它们的构建方法。
class ConnectivityUtils {
static Future<bool> hasConnection() async {
bool hasWifi = false;
bool hasMobileConnection = false;
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile) {
hasMobileConnection = true;
} else if (connectivityResult == ConnectivityResult.wifi) {
hasWifi = true;
}
return hasWifi || hasMobileConnection;
}
}
你可以使用这个class,你可以在调用API之前调用hasConnection()
如果为假,则可以显示无连接屏幕
我在我的项目中使用 connectivity 包来检查互联网连接。
文件main.dart
代码:
StreamProvider<ConnectivityResult>(
create: (context) =>
InternetConnectionService().connectionStatusController.stream,
child: MaterialApp(
.....
并且在每个屏幕上我都这样检查互联网连接:
bool hasConnection;
void checkConnectivity(context) async {
var connectivityResult = Provider.of<ConnectivityResult>(context);
if (connectivityResult == ConnectivityResult.none ||
connectivityResult == null) {
setState(() {
hasConnection = false;
});
} else {
setState(() {
hasConnection = true;
});
}
}
Widget build(BuildContext context) {
checkConnectivity(context);
return hasConnection == true
? Scaffold()
: NoInternetScreen();
}
我如何在全局而不是在每个屏幕上从根或一个小部件上检查连接并且不显示连接屏幕?
在您的 MaterialApp 小部件中,有一个构建器。您可以使用构建器将路径包装在任何小部件中。尝试这样做:
MaterialApp(
...
builder: (context, child) {
return StreamBuilder<ConnectivityResult>(
stream: InternetConnectionService().connectionStatusController.stream,
builder: (context, snapshot) {
final conenctivityResult = snapshot.data;
if (connectivityResult == ConnectivityResult.none || connectivityResult == null) return NoInternetScreen();
return child;
}
);
}
);
现在您不必在其他文件中添加任何互联网逻辑。您可以简单地编写排除它们的构建方法。
class ConnectivityUtils {
static Future<bool> hasConnection() async {
bool hasWifi = false;
bool hasMobileConnection = false;
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile) {
hasMobileConnection = true;
} else if (connectivityResult == ConnectivityResult.wifi) {
hasWifi = true;
}
return hasWifi || hasMobileConnection;
}
}
你可以使用这个class,你可以在调用API之前调用hasConnection()
如果为假,则可以显示无连接屏幕