gpt4 book ai didi

flutter - 未找到在 ConnectionStatus 库的 channel 上监听方法的实现

转载 作者:行者123 更新时间:2023-12-05 07:22:23 25 4
gpt4 key购买 nike

我正在尝试使用 FlutterCheckInternetConnectivity在我们的应用程序上,运行应用程序后出现此错误:

plugins.flutter.io/connectivity_status:
MissingPluginException(No implementation found for method listen on channel
plugins.flutter.io/connectivity_status)

这部分代码:

void dispose() {
connectionChangeController.close();
}

ConnectionStatusSingleton 类:

class ConnectionStatusSingleton {
static final ConnectionStatusSingleton _singleton = new ConnectionStatusSingleton._internal();
ConnectionStatusSingleton._internal();
static ConnectionStatusSingleton getInstance() => _singleton;
bool hasConnection = false;
StreamController connectionChangeController = new StreamController.broadcast();
final Connectivity _connectivity = Connectivity();
void initialize() {
_connectivity.onConnectivityChanged.listen(_connectionChange);
checkConnection();
}
Stream get connectionChange => connectionChangeController.stream;
void dispose() {
connectionChangeController.close();
}
void _connectionChange(ConnectivityResult result) {
checkConnection();
}
Future<bool> checkConnection() async {
bool previousConnection = hasConnection;
try {
final result = await InternetAddress.lookup('google.com');
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
hasConnection = true;
} else {
hasConnection = false;
}
} on SocketException catch(_) {
hasConnection = false;
}
if (previousConnection != hasConnection) {
connectionChangeController.add(hasConnection);
}
return hasConnection;
}
}

我实现了这个库:

main() {
ConnectionStatusSingleton connectionStatus = ConnectionStatusSingleton.getInstance();
connectionStatus.initialize();

runApp(MaterialApp(
...
));
}

_FragmentPostsState 小部件类:

class _FragmentPostsState extends State<FragmentPosts> {
StreamSubscription _connectionChangeStream;
bool isOffline = false;

@override
void initState() {
super.initState();
ConnectionStatusSingleton connectionStatus = ConnectionStatusSingleton.getInstance();
_connectionChangeStream = connectionStatus.connectionChange.listen(connectionChanged);
}

@override
Widget build(BuildContext context) {
return (isOffline)
? Center(...)
: PostPage();
}

void connectionChanged(dynamic hasConnection) {
setState(() {
print('connection changed ...');
isOffline = !hasConnection;
});
}
}

最佳答案

我遇到了同样的问题,最终我从命令行重新启动了应用程序,这解决了问题。

关于flutter - 未找到在 ConnectionStatus 库的 channel 上监听方法的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56546254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com