check internet connection in webview flutter code example
Example: 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) {
// I am connected to a mobile network.
} else if (connectivityResult == ConnectivityResult.wifi) {
// I am connected to a wifi network.
initialUrl: "https://google.com/",
onWebViewCreated: (WebViewController webViewController){
_controller.complete(webViewController);
},}
else{
//i am not connected to any network
}
) );
}
}