Flutter 检测网络连接 监听网络变化
Posted zhaofeis
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flutter 检测网络连接 监听网络变化相关的知识,希望对你有一定的参考价值。
Flutter 检测网络 connectivity
Flutter 检测网络完整 demo
import ‘package:connectivity/connectivity.dart‘;
_NetworkPageState createState() => _NetworkPageState(); }
class _NetworkPageState extends State<NetworkPage> { String _state;
var _subscription;
@override initState() {
super.initState();
_subscription=Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
// Got a new connectivity status!
if (result == ConnectivityResult.mobile) {
setState(() { _state="手机网络";
});
// I am connected to a mobile network. } else if (result == ConnectivityResult.wifi) {
setState(() { _state="Wifi 网络";
});
// I am connected to a wifi network. }else{
setState(() { _state="没有网络";
}); }
}); }
@override dispose() {
super.dispose();
_subscription.cancel(); }
Widget build(BuildContext context) {
return Scaffold( appBar: AppBar(
title: Text("检测网络变化"), ),
body:Text("${_state}"), );
} }
以上是关于Flutter 检测网络连接 监听网络变化的主要内容,如果未能解决你的问题,请参考以下文章