我是 Flutter 和连接性
的新手。我尝试使用 connectivity
包,但我想在现有应用程序中实现。如果有可用连接则打开HomePage()
状态,否则引用另一个状态
。 请帮助我。
这是我的代码 https://pastebin.com/3wbDiF8j (主 Dart )。 https://pastebin.com/vPUYUdgc (noInternet.dart)
最佳答案
使用Connectivity Plus使用 StreamBuilder 打包并返回 MaterialApp。 这样,您就不必在每个页面上都检查连接。
StreamBuilder(
stream: Connectivity().onConnectivityChanged,
builder: (context, AsyncSnapshot<ConnectivityResult> snapshot) {
return snapshot.data == ConnectivityResult.mobile ||
snapshot.data == ConnectivityResult.wifi
? OnlineMaterialApp()
: OfflineMaterialApp();
},
),
关于flutter - 检查 flutter 中的连接并根据连接状态更改状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68030497/