前言
在Android中會(huì)在基礎(chǔ)類中監(jiān)聽手機(jī)的網(wǎng)絡(luò)狀態(tài)结执,并作出相應(yīng)的提示谴麦,在Flutter 中也提供了相應(yīng)的package,但是略有不同宇挫;
1.依賴
dependencies:
connectivity: ^0.3.2
2.導(dǎo)入
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
3.簡單使用
檢查當(dāng)前網(wǎng)絡(luò)狀態(tài)
注意:在Android上荧止,這并不保證連接到Internet稼跳。例如盟庞,該應(yīng)用程序可能有WiFi接入,但可能是VPN或沒有接入的酒店WiFi汤善。也就是說網(wǎng)絡(luò)狀態(tài)是'wifi',也不代表你的手機(jī)能進(jìn)行網(wǎng)絡(luò)訪問什猖。
import 'package:connectivity/connectivity.dart';
var connectivityResult = await (new Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile) {
// I am connected to a mobile network.
} else if (connectivityResult == ConnectivityResult.wifi) {
// I am connected to a wifi network.
}
監(jiān)聽網(wǎng)絡(luò)狀態(tài)
請(qǐng)注意,不應(yīng)該使用當(dāng)前的網(wǎng)絡(luò)狀態(tài)來決定是否可以可靠地進(jìn)行網(wǎng)絡(luò)連接红淡。
網(wǎng)絡(luò)層可能會(huì)出現(xiàn)超時(shí)和錯(cuò)誤不狮,需要在應(yīng)用程序代碼中做好異常處理;
import 'package:connectivity/connectivity.dart';
initState() {
subscription = new Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
// Got a new connectivity status!
})
}
// Be sure to cancel subscription after you are done
dispose() {
subscription.cancel();
}
4.例子
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:connectivity/connectivity.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
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 _connectionStatus = 'Unknown';
final Connectivity _connectivity = Connectivity();
StreamSubscription<ConnectivityResult> _connectivitySubscription;
@override
void initState() {
super.initState();
initConnectivity();
_connectivitySubscription =
_connectivity.onConnectivityChanged.listen((ConnectivityResult result) {
setState(() => _connectionStatus = result.toString());
});
}
@override
void dispose() {
_connectivitySubscription.cancel();
super.dispose();
}
//平臺(tái)消息是異步的在旱,所以我們用異步方法初始化荤傲。
Future<Null> initConnectivity() async {
String connectionStatus;
//平臺(tái)消息可能會(huì)失敗冀值,因此我們使用Try/Catch PlatformException猎贴。
try {
connectionStatus = (await _connectivity.checkConnectivity()).toString();
} on PlatformException catch (e) {
print(e.toString());
connectionStatus = 'Failed to get connectivity.';
}
// 如果在異步平臺(tái)消息運(yùn)行時(shí)從樹中刪除了該小部件,
// 那么我們希望放棄回復(fù)安接,而不是調(diào)用setstate來更新我們不存在的外觀俊嗽。
if (!mounted) {
return;
}
setState(() {
_connectionStatus = connectionStatus;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(child: Text('Connection Status: $_connectionStatus\n')),
);
}
}
5.tips
當(dāng) wifi 和 移動(dòng)網(wǎng)絡(luò) 同時(shí)存在時(shí)雾家,監(jiān)聽中提示當(dāng)前網(wǎng)絡(luò)wifi;并且此時(shí)斷開或者連接移動(dòng)網(wǎng)絡(luò)無回調(diào)
6.資料參考
https://pub.dartlang.org/packages/connectivity
- 這個(gè)網(wǎng)址提供了很多兼容Android 和 IOS 的 package;