connectivity flutter code example
Example 1: how to handle the case of no internet connection in flutter
I found that just using the connectivity package was not enough to tell if the internet was available or not. In Android it only checks if there is WIFI or if mobile data is turned on, it does not check for an actual internet connection . During my testing, even with no mobile signal ConnectivityResult.mobile would return true.
With IOS my testing found that the connectivity plugin does correctly detect if there is an internet connection when the phone has no signal, the issue was only with Android.
The solution I found was to use the data_connection_checker package along with the connectivity package. This just makes sure there is an internet connection by making requests to a few reliable addresses, the default timeout for the check is around 10 seconds.
My finished isInternet function looked a bit like this:
Future<bool> isInternet() async {
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile) {
if (await DataConnectionChecker().hasConnection) {
return true;
} else {
return false;
}
} else if (connectivityResult == ConnectivityResult.wifi) {
if (await DataConnectionChecker().hasConnection) {
return true;
} else {
return false;
}
} else {
return false;
}
}
The if (await DataConnectionChecker().hasConnection) part is the same for both mobile and wifi connections and should probably be moved to a separate function. I've not done that here to leave it more readable.
Example 2: check connectivity flutter for webview
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:connectivity/connectivity.dart';
void main() => runApp(MyApp());
var connectivityResult = await (Connectivity().checkConnectivity());
await(Future<ConnectivityResult> checkConnectivity) {
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.yellow
),
home: MyHomePage(title: 'Flutter Demooo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final Completer<WebViewController> _controller=Completer<WebViewController>();
@override
Widget build(BuildContext context) {
return Scaffold(
body: WebView(
if (connectivityResult == ConnectivityResult.mobile) {
} else if (connectivityResult == ConnectivityResult.wifi) {
initialUrl: "https://google.com/",
onWebViewCreated: (WebViewController webViewController){
_controller.complete(webViewController);
},}
else{
}
) );
}
}
Example 3: flutter check internet connection
import 'package:connectivity/connectivity.dart';
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile) {
} else if (connectivityResult == ConnectivityResult.wifi) {
}