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

标签 flutter

我正在尝试使用 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/

相关文章:

android-studio - 如何在Flutter中删除默认的白色启动画面。我根本不需要启动画面

arrays - Flutter - Json 对象数组

flutter - 如何在提交时将文本字段更改为另一个小部件?

flutter 图表 : using double in tick spec

sorting - 如何使用 Dart 对列表进行排序?

flutter - 如何按值对列表中的映射进行排序

Flutter - 以纵向和横向显示图像的空白

flutter - Flutter 的 pubspec.yaml 中依赖版本号前的脱字符号 (^) 是什么?

flutter - 如何使用填充水平滚动到屏幕的最后/开始?

Android Studio Flutter - 虚拟设备正在加载或卡住