检测网络
Posted zhaofeis
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了检测网络相关的知识,希望对你有一定的参考价值。
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 = ‘无网络‘;});
}
}
}
以上是关于检测网络的主要内容,如果未能解决你的问题,请参考以下文章
VSCode自定义代码片段14——Vue的axios网络请求封装