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

检测网络

时间:2020-02-27 19:12:58      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:var   ase   packages   ret   bar   creates   uil   mat   children   

Flutter 检测网络
  https://pub.dev/packages/connectivity
 
案例代码
 
import ‘package:flutter/material.dart‘;

import ‘package:connectivity/connectivity.dart‘;

class CameraPage extends StatefulWidget{
CameraPage({Key key});
_CameraPage createState() => _CameraPage();
}

class _CameraPage extends State {

var subscription ;
var whatNetwork = ‘未检测网络‘;
initState() {
super.initState();
getNetwork();
}
dispose() {
super.dispose();

subscription .cancel();
}
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text(‘设备硬件‘)
),
body: ListView(
children: <Widget>[
Text(‘${whatNetwork}‘),
]
)
);
}

// 网络监测
getNetwork() async{
subscription = await (Connectivity().checkConnectivity());
// Got a new connectivity status!
switch(subscription) {
case ConnectivityResult.mobile:
setState(() {whatNetwork = ‘手机网络‘;});
break;
case ConnectivityResult.wifi:
setState(() {whatNetwork = ‘网络WIFI‘;});
break;
default:
setState(() {whatNetwork = ‘无网络‘;});
}
}
}

检测网络

标签:var   ase   packages   ret   bar   creates   uil   mat   children   

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

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