问题描述
借助此连接插件,我可以获取连接状态,即移动网络,wifi或不使用以下代码:
With the help of this Connectivity Plugin, I am able to get the connection status i.e. mobile network, wifi or none using the following code:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:connectivity/connectivity.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _connectionStatus = 'Unknown';
final Connectivity _connectivity = new 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();
}
Future<Null> initConnectivity() async {
String connectionStatus;
try {
connectionStatus = (await _connectivity.checkConnectivity()).toString();
} on PlatformException catch (e) {
print(e.toString());
connectionStatus = 'Failed to get connectivity.';
}
if (!mounted) {
return;
}
setState(() {
_connectionStatus = connectionStatus;
});
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: const Text('Plugin example app'),
),
body: new Center(
child: new Text('Connection Status: $_connectionStatus\n')),
);
}
}
现在,我要在手机连接到wifi时获取Wifi的名称.详细说明:假设用户已将其电话连接到名为"Home Wifi"的wifi,根据我写的代码,无论手机是否连接到wifi,我都只能得到,我也想获取名称如果手机已连接到wifi,即家用Wifi",则为wifi.
Now what I want is to get the name of the Wifi when the phone is connected to wifi.Detailed Description: Suppose the user has connected his/her phone with a wifi named "Home Wifi", from the code I have wriiten I am only able to get if the phone is connected to wifi or not, I also want to get the name of the wifi if the phone is connected to the wifi i.e. "Home Wifi".
推荐答案
它只是调用 getWifiName().在连接插件的0.3.2
版本上可用.
It's just calling getWifiName(). It's available on the 0.3.2
version of the connectivity plugin.
在iOS中,使用此解决方案需要此答案中所述的步骤.
In iOS, using this solution requires the steps described in this answer.
这篇关于如何在Flutter中获取当前连接的wifi的wifi名称(SSID)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!