Internet Availability
A Flutter package which check the internet availability and push a new page on top of the current route if internet is not available. Once the internet is available that page will automatically pop to previous route.
Note: The package will only work with go_router
Features
Getting started
flutter pub add internet_availability
flutter pub get
Usage
- In your
routes
list add one more route
GoRoute(
path: NoInternetPage.routeName,
builder: (context, state) => const NoInternetPage(),
),
The route should not be a nested route. You can provide any value to path
property and even your own Widget to builder
property.
- add
redirect
andrefreshListenable
property of GoRouter and paste below code
redirect: (context, state) {
switch (InternetAvailabilityConfig.showNoInternetPage()) {
case true:
InternetAvailabilityConfig.prevRoute = state.subloc;
return NoInternetPage.routeName; //page route path
case false:
return InternetAvailabilityConfig.prevRoute;
default:
return null;
}
},
refreshListenable: RefreshStream(
InternetAvailabilityConfig.internetAvailabilityCubit.stream),
Note: For
go_router
version lower than 5.0 useGoRouterRefreshStream
andredirect
will only takestate
as an argument.
refreshListenable: GoRouterRefreshStream(
InternetAvailabilityConfig.internetAvailabilityCubit.stream),
Additional Information
check the example repository of the project for complete code.