Flutter: Check for Internet Connection

8/25/2020 10:13:18 PM

Check to see if you can connect to the internet.

import 'package:flutter/material.dart';
import 'dart:io';

class CheckConnectivity extends StatelessWidget {
  Future checkConnectivity() async {
    print("checkConnectivity");

    try {
      InternetAddress.lookup('goo gle.com').then((value) {
        print("Connection WORKS");
        print(value);
      });
    } catch (ex) {
      print("Connection FAILED");
      print(ex);
    }
  }

  @override
  Widget build(BuildContext context) {
    checkConnectivity();

    return new MaterialApp(
        home: Container(
        child: Text("running tests"),
    ));
  }
}