hey I have created an app in flutter it has a download page just like youtube
what I want to do is when the app loads it should check the internet connection and if it is not there it should redirect the user to the download screen
This is the error
../../../../../.pub-cache/hosted/pub.dartlang.org/flutter_translate-3.1.0/lib/src/utils/device_locale.dart:11:49: Error: Property 'window' cannot be accessed on 'WidgetsBinding?' because it is potentially null.
- 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../../../../../FlutterDev/flutter/packages/flutter/lib/src/widgets/binding.dart').
Try accessing using ?. instead.
final deviceLocales = WidgetsBinding.instance.window.locales;
^^^^^^
../../../../../.pub-cache/hosted/pub.dartlang.org/percent_indicator-4.2.1/lib/linear_percent_indicator.dart:162:5: Warning: The class 'WidgetsBinding' cannot be null.
Try replacing '?.' with '.'
WidgetsBinding?.instance.addPostFrameCallback((_) {
^^^^^^^^^^^^^^
../../../../../.pub-cache/hosted/pub.dartlang.org/percent_indicator-4.2.1/lib/linear_percent_indicator.dart:162:30: Error: Method 'addPostFrameCallback' cannot be called on 'WidgetsBinding?' because it is potentially null.
- 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../../../../../FlutterDev/flutter/packages/flutter/lib/src/widgets/binding.dart').
Try calling using ?. instead.
WidgetsBinding?.instance.addPostFrameCallback((_) {
^^^^^^^^^^^^^^^^^^^^
FAILURE: Build failed with an exception.
* Where:
Script '/Users/samarthverulkar/FlutterDev/flutter/packages/flutter_tools/gradle/flutter.gradle' line: 1102
* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command '/Users/samarthverulkar/FlutterDev/flutter/bin/flutter'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 2m 1s
Exception: Gradle task assembleDebug failed with exit code 1
CodePudding user response:
Create a bool
bool internetAvailable = true;
Use internet connection checker internet_connection_checker
InternetConnectionChecker().checkInterval = Duration(seconds: 10);
InternetConnectionChecker().onStatusChange.listen(
(InternetConnectionStatus status) {
switch (status) {
case InternetConnectionStatus.connected:
// ignore: avoid_print
print('Data connection is available.');
internetAvailable = true;
setState(() {});
break;
case InternetConnectionStatus.disconnected:
// ignore: avoid_print
print('You are disconnected from the internet.');
internetAvailable = false;
setState(() {});
break;
}
},
);
Then based on the internetAvailable add the screens
return internetAvailable? Center(child:Text("no internet")):HomePage()