【问题标题】:Using connectivity package in Flutter with provider将 Flutter 中的连接包与提供程序一起使用
【发布时间】:2021-03-01 10:55:18
【问题描述】:

我正在创建一个新的 Flutter 项目。

我想使用 Provider 检查整个应用程序的互联网连接状态。

我在 pubspec.yaml 文件中包含了“connectivity”和“provider”这两个包。

然后我将 main.dart 更改如下,以包含 Connectivity 插件的流提供程序:

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        StreamProvider(
            create: (context) => Connectivity().onConnectivityChanged),
      ],
      child: MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'My New App',
        theme: ThemeData(
          backgroundColor: Colors.white,
        ),
        home: MyHomePage(title: 'My New App Login'),
      ),
    );
  }
}

在我想检查互联网连接状态的每个新课程中我应该怎么做?

我知道我必须使提供者生效:

var connectionStatus;
connectionStatus = Provider.of<ConnectivityResult>(context);

但是,我应该怎么做才能收听每个班级的连接状态?

【问题讨论】:

  • Connectivity 只会检查您是否连接到 wifi 网络,如果您有互联网连接则不会。更好的选择是使用 http 包并 ping 一个公共站点。
  • @rkdupr0n,谢谢,我知道,但我想先如何使用提供程序检查连接,先学习如何实现,然后如果有互联网连接,我会实施
  • 刚刚找到这个包来检查互联网连接。 pub.dev/packages/data_connection_checker 干杯!

标签: flutter


【解决方案1】:

无论我做什么,我都会不断收到错误消息。一直给我null 结果。所以我想我会看到背后的代码,你瞧,我发现了什么......

我真的不知道问题是什么,或者如果这只是我这边的问题,撕掉。

编辑:

我跟着 wut moneer 做的,它有效,哈哈。

import 'package:connectivity/connectivity.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        StreamProvider(create: (context) => Connectivity().onConnectivityChanged),
      ],
      child: MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'My New App',
        theme: ThemeData(
          backgroundColor: Colors.white,
        ),
        home: MyHomePage(title: 'My New App Login'),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _connectivityType = 'Init';

  void _getConnectionStatus(ConnectivityResult con) {
    print(con);
    setState(() {
      switch (con) {
        case ConnectivityResult.mobile:
          _connectivityType = 'Mobile';
          break;
        case ConnectivityResult.wifi:
          _connectivityType = 'Wifi';
          break;
        case ConnectivityResult.none:
          _connectivityType = 'None';
          break;
        default:
          _connectivityType = 'Unknown';
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    final con = Provider.of<ConnectivityResult>(context);
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_connectivityType',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => _getConnectionStatus(con),
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

编辑 2: 如果你想检查你的互联网连接,this 是你的软件包。

【讨论】:

    【解决方案2】:

    ConnectivityResult 是一个枚举,所以你需要比较它。

    
    /// Connection status check result.
    enum ConnectivityResult {
      /// WiFi: Device connected via Wi-Fi
      wifi,
    
      /// Mobile: Device connected to cellular network
      mobile,
    
      /// None: Device not connected to any network
      none
    }
    
    

    您还应该检查该值是否为null 我不太记得为什么

    【讨论】:

    • 让我详细说明一下。我在Consumer 中使用它,但您的Provider.of&lt;ConnectivityResult&gt;(context) 也应该可以使用。 of 有一个参数 listen 它默认为 true 如果你在 build 方法中调用,它应该在连接改变时重建。在您的 StreamProvider 构造函数中,您还应该传递 catchError 以防万一。
    • 哦...有道理
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 2021-09-30
    相关资源
    最近更新 更多