Flutter How can I check internet connection through the whole app NOT only in one particular Class, and prompt pop-up dialog when it lost?

721

You need to use the Connectivity Plugin.

import 'dart:io';
try {
  final result = await InternetAddress.lookup('google.com');
  if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
    print('connected');
  }
} on SocketException catch (_) {
  print('not connected');
}
Share:
721
Rock
Author by

Rock

Updated on December 24, 2022

Comments

  • Rock
    Rock over 1 year

    How can I continuously check internet connection for whole application(I mean all classes and widgets) and prompt the pop-up dialog when the connection is lost. Please provide an example if it is possible.

  • Rock
    Rock over 3 years
    How can i implement it in Main() and also Main does not have context so I cant prompt pop-up there ?