[英]Using connectivity package in Flutter with provider
我正在創建一個新的 Flutter 項目。
我想使用 Provider 檢查整個應用程序的 Internet 連接狀態。
我在 pubspec.yaml 文件中包含了“connectivity”和“provider”這兩個包。
然后我改變了 main.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 上,我應該怎么做?
我知道我必須使提供者成立:
var connectionStatus;
connectionStatus = Provider.of<ConnectivityResult>(context);
但是,我應該怎么做才能監聽每個 class 的連接狀態?
無論我做什么,我都不斷收到錯誤。 一直給我一個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:如果您想檢查您的互聯網連接,這就是您的 package。
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
我不太記得為什么
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.