码迷,mamicode.com
首页 > 其他好文 > 详细

Flutter 检测网络连接 监听网络变化

时间:2020-04-23 21:02:52      阅读:324      评论:0      收藏:0      [点我收藏+]

标签:mob   cti   sub   else   dispose   got   ges   result   err   

Flutter 检测网络  connectivity

 

Flutter 检测网络完整 demo

import ‘package:flutter/material.dart‘;
import ‘package:connectivity/connectivity.dart‘;

 

class NetworkPage extends StatefulWidget { NetworkPage({Key key}) : super(key: key);

 

_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(); }

 

@override
Widget build(BuildContext context) {

 

return Scaffold( appBar: AppBar(

title: Text("检测网络变化"), ),

body:Text("${_state}"), );

} }

 

Flutter 检测网络连接 监听网络变化

标签:mob   cti   sub   else   dispose   got   ges   result   err   

原文地址:https://www.cnblogs.com/zhaofeis/p/12763092.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!