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')),
);
}
}
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".
To find your WiFi network name and password: Make sure you're connected to your WiFi network. In the taskbar, right-click the WiFi icon, and then select Open Network and Sharing Center. Next to Connections, select your WiFi network name.
SSID is simply the technical term for a Wi-Fi network name. When you set up a wireless home network, you give it a name to distinguish it from other networks in your neighbourhood. You'll see this name when you connect your devices to your wireless network.
You can use dart's connectivity_plus package which is maintained by Google team, according to its documentation : "This plugin allows Flutter apps to discover network connectivity and configure themselves accordingly. It can distinguish between cellular vs WiFi connection. This plugin works for iOS and Android."
It's just calling getWifiName(), available in the wifi_info_flutter plugin. This method used to be available in the connectivity plugin, but it has been moved to this new plugin by the end of 2020.
In iOS, using this solution requires the steps described in this answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With