Farmers in yards world, beautiful application experience, from the programmer for the processing of detail, and self requirements state, agriculture in the yard a member of the young people is busy, every day, every week, can leave some footprints, is the creation of content, there is a persistent, is I don’t know why, if you lost, might as well to Chou Chou code track of farmers.
If you are interested, you can follow the public account Biglead to get the latest learning materials.
By using the plug-in Connectivity, you can obtain whether the current device is connected to the Internet and whether the current network type is WIFI or mobile network
So the first step is to load the plug-in, first of allThe pub warehouseThe latest version of the search plugin
Then add the latest version to the Flutter project configuration file
Then import the header file when you use it
import 'package:connectivity/connectivity.dart';
Copy the code
// Get the network type
String _netType;
void isConnectedType(a) async {
// Get the network connection type
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile) {
// The network type is mobile
_netType = "4G";
} else if (connectivityResult == ConnectivityResult.wifi) {
// The network type is WIFI
_netType = "WIFI";
}else{
_netType = "Unjoined";
}
setState(() {});
}
Copy the code
Of course, you can directly determine whether the network is connected based on the connectivityResult
// Check whether the network is connected
Future<bool> isConnected(a) async {
var connectivityResult = await (Connectivity().checkConnectivity());
returnconnectivityResult ! = ConnectivityResult.none; }Copy the code
Of course, when your network changes, you can also set up monitoring here
// Add network switch listener
connectLiserner() async {
/ / get the Stream
Stream<ConnectivityResult> resultStream =
Connectivity().onConnectivityChanged;
// Add a listener
resultStream.listen((event) {
//
});
}
Copy the code
The completion of
Not limited to thinking, not limited to language restrictions, is the highest realm of programming.
With xiaobian character, must be to record a set of video, and then upload
If you are interested, you can follow the public account Biglead to get the latest learning materials