fluttercommunity / plus_plugins

Flutter Community Plus Plugins
BSD 3-Clause "New" or "Revised" License
1.61k stars 976 forks source link

Null check operator used on a null value _CastError #524

Closed TheGlorySaint closed 2 years ago

TheGlorySaint commented 3 years ago

I want to implement, that when the User has no Internet Connection(Not Connected to Wlan, Ethernet or Cellular) to display a separate Page. For that I use the connectivity_plus Plugin in Version 2.0.1, Flutter 2.5.2 and NO Null-Sound-Safety.

Switching the Smartphone to Airplane mode from Wlan or Cellular Connection the InternetConnection Page is displayed but disabling Airplanemode and having Cellular or Wlan I get The following _CastError was thrown building Builder(dirty): Null check operator used on a null value.

I tried to use Conditional Operators that didn't helped added the home function to the else Statement and added also there the Conditional Operator for the home and for the initialRoute that also didn't helped and now I'm clueless and what I can do.

flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
flutter: The following _CastError was thrown building Builder(dirty):
flutter: Null check operator used on a null value
flutter:
flutter: The relevant error-causing widget was:
flutter:   MaterialApp MaterialApp:file:///Users/blackbox/Desktop/name/example/lib/main.dart:170:18
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #0      _WidgetsAppState._onGenerateRoute.<anonymous closure> (package:flutter/src/widgets/app.dart:1353:48)
flutter: #1      MaterialPageRoute.buildContent (package:flutter/src/material/page.dart:53:55)
flutter: #2      MaterialRouteTransitionMixin.buildPage (package:flutter/src/material/page.dart:106:27)
flutter: #3      _ModalScopeState.build.<anonymous closure>.<anonymous closure> (package:flutter/src/widgets/routes.dart:851:55)
flutter: #4      Builder.build (package:flutter/src/widgets/basic.dart:8004:48)
flutter: #5      StatelessElement.build (package:flutter/src/widgets/framework.dart:4739:28)
flutter: #6      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4665:15)
flutter: #7      Element.rebuild (package:flutter/src/widgets/framework.dart:4355:5)
flutter: #8      StatelessElement.update (package:flutter/src/widgets/framework.dart:4746:5)
flutter: #9      Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #10     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6202:14)
flutter: #11     Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #12     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6202:14)
flutter: #13     Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #14     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4690:16)
flutter: #15     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4840:11)
flutter: #16     Element.rebuild (package:flutter/src/widgets/framework.dart:4355:5)
flutter: #17     StatefulElement.update (package:flutter/src/widgets/framework.dart:4872:5)
flutter: #18     Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #19     RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5700:32)
flutter: #20     MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6356:17)
flutter: #21     Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #22     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4690:16)
flutter: #23     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4840:11)
flutter: #24     Element.rebuild (package:flutter/src/widgets/framework.dart:4355:5)
flutter: #25     StatefulElement.update (package:flutter/src/widgets/framework.dart:4872:5)
flutter: #26     Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #27     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6202:14)
flutter: #28     Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #29     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4690:16)
flutter: #30     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4840:11)
flutter: #31     Element.rebuild (package:flutter/src/widgets/framework.dart:4355:5)
flutter: #32     StatefulElement.update (package:flutter/src/widgets/framework.dart:4872:5)
flutter: #33     Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #34     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6202:14)
flutter: #35     Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #36     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4690:16)
flutter: #37     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4840:11)
flutter: #38     Element.rebuild (package:flutter/src/widgets/framework.dart:4355:5)
flutter: #39     StatefulElement.update (package:flutter/src/widgets/framework.dart:4872:5)
flutter: #40     Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #41     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6202:14)
flutter: #42     Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #43     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4690:16)
flutter: #44     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4840:11)
flutter: #45     Element.rebuild (package:flutter/src/widgets/framework.dart:4355:5)
flutter: #46     StatefulElement.update (package:flutter/src/widgets/framework.dart:4872:5)
flutter: #47     Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #48     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4690:16)
flutter: #49     Element.rebuild (package:flutter/src/widgets/framework.dart:4355:5)
flutter: #50     StatelessElement.update (package:flutter/src/widgets/framework.dart:4746:5)
flutter: #51     Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #52     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4690:16)
flutter: #53     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4840:11)
flutter: #54     Element.rebuild (package:flutter/src/widgets/framework.dart:4355:5)
flutter: #55     StatefulElement.update (package:flutter/src/widgets/framework.dart:4872:5)
flutter: #56     Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #57     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6202:14)
flutter: #58     Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #59     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6202:14)
flutter: #60     Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #61     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4690:16)
flutter: #62     Element.rebuild (package:flutter/src/widgets/framework.dart:4355:5)
flutter: #63     ProxyElement.update (package:flutter/src/widgets/framework.dart:5020:5)
flutter: #64     _InheritedNotifierElement.update (package:flutter/src/widgets/inherited_notifier.dart:181:11)
flutter: #65     Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #66     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6202:14)
flutter: #67     Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #68     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4690:16)
flutter: #69     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4840:11)
flutter: #70     Element.rebuild (package:flutter/src/widgets/framework.dart:4355:5)
flutter: #71     StatefulElement.update (package:flutter/src/widgets/framework.dart:4872:5)
flutter: #72     Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #73     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4690:16)
flutter: #74     Element.rebuild (package:flutter/src/widgets/framework.dart:4355:5)
flutter: #75     ProxyElement.update (package:flutter/src/widgets/framework.dart:5020:5)
flutter: #76     Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #77     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4690:16)
flutter: #78     Element.rebuild (package:flutter/src/widgets/framework.dart:4355:5)
flutter: #79     ProxyElement.update (package:flutter/src/widgets/framework.dart:5020:5)
flutter: #80     Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #81     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4690:16)
flutter: #82     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4840:11)
flutter: #83     Element.rebuild (package:flutter/src/widgets/framework.dart:4355:5)
flutter: #84     StatefulElement.update (package:flutter/src/widgets/framework.dart:4872:5)
flutter: #85     Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #86     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4690:16)
flutter: #87     Element.rebuild (package:flutter/src/widgets/framework.dart:4355:5)
flutter: #88     StatelessElement.update (package:flutter/src/widgets/framework.dart:4746:5)
flutter: #89     Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #90     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4690:16)
flutter: #91     Element.rebuild (package:flutter/src/widgets/framework.dart:4355:5)
flutter: #92     StatelessElement.update (package:flutter/src/widgets/framework.dart:4746:5)
flutter: #93     Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #94     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6202:14)
flutter: #95     Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #96     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4690:16)
flutter: #97     Element.rebuild (package:flutter/src/widgets/framework.dart:4355:5)
flutter: #98     ProxyElement.update (package:flutter/src/widgets/framework.dart:5020:5)
flutter: #99     Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #100    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4690:16)
flutter: #101    Element.rebuild (package:flutter/src/widgets/framework.dart:4355:5)
flutter: #102    ProxyElement.update (package:flutter/src/widgets/framework.dart:5020:5)
flutter: #103    Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #104    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4690:16)
flutter: #105    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4840:11)
flutter: #106    Element.rebuild (package:flutter/src/widgets/framework.dart:4355:5)
flutter: #107    StatefulElement.update (package:flutter/src/widgets/framework.dart:4872:5)
flutter: #108    Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #109    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4690:16)
flutter: #110    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4840:11)
flutter: #111    Element.rebuild (package:flutter/src/widgets/framework.dart:4355:5)
flutter: #112    StatefulElement.update (package:flutter/src/widgets/framework.dart:4872:5)
flutter: #113    Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #114    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4690:16)
flutter: #115    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4840:11)
flutter: #116    Element.rebuild (package:flutter/src/widgets/framework.dart:4355:5)
flutter: #117    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2620:33)
flutter: #118    WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:882:21)
flutter: #119    RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:319:5)
flutter: #120    SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1143:15)
flutter: #121    SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1080:9)
flutter: #122    SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:996:5)
flutter: #126    _invoke (dart:ui/hooks.dart:166:10)
flutter: #127    PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:270:5)
flutter: #128    _drawFrame (dart:ui/hooks.dart:129:31)
flutter: (elided 3 frames from dart:async)
import 'dart:async';
import 'dart:io';

import 'package:app/env.dart';
import 'package:app/generated/l10n.dart';
import 'package:app/provider/app_clock.dart';
import 'package:app/provider/auth.dart';
import 'package:app/provider/internet_connection.dart';
import 'package:app/provider/local_auth.dart';
import 'package:app/provider/therapist.dart';
import 'package:app/provider/therapy.dart';
import 'package:app/provider/user.dart';
import 'package:app/routes.dart';
import 'package:app/services.dart';
import 'package:app/theme.dart';
import 'package:app/ui/screens/setup/internet_connection.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:in_app_purchase_android/in_app_purchase_android.dart';
import 'package:localizely_sdk/localizely_sdk.dart';
import 'package:provider/provider.dart';

import 'util/http_overrides.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  if (defaultTargetPlatform == TargetPlatform.android) {
    // For the GooglePlay billing library 2.0 on Android, it is mandatory to call
    // [enablePendingPurchases](https://developer.android.com/reference/com/android/billingclient/api/BillingClient.Builder.html#enablependingpurchases)
    // as part of initializing the app.
    InAppPurchaseAndroidPlatformAddition.enablePendingPurchases();
  }

  if (AppEnv.LOCALIZELY_SDK_TOKEN.isNotEmpty) {
    Localizely.init(
      AppEnv.LOCALIZELY_SDK_TOKEN,
      AppEnv.LOCALIZELY_DISTRUBUTION_ID,
    ); // Init sdk

    Localizely.setPreRelease(true);
    await S.load(S.delegate.supportedLocales.first);

    try {
      dynamic result = await Localizely.updateTranslations();
      AppEnv.localizelySDKenabled = true;
      AppEnv.currentLocalizelyReleaseId = result.newReleaseId;
    } catch (e) {
      AppEnv.localizelySDKenabled = false;
    }
  } else {
    await S.load(S.delegate.supportedLocales.first);
  }

  await SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
  ]);

  HttpOverrides.global = MyHttpOverrides();

  bool isFirebaseInit = false;
  String errorMessage = '';
  try {
    await Firebase.initializeApp();
    // All Services and Providers are being initialized
    setupServices(); //needs Firebase
    // And we are waiting until that's done
    print("Wating for services");

    await services.allReady(timeout: Duration(seconds: 60));

    print("Services initialized");

    // Force disable Crashlytics collection while doing every day development.
    // Temporarily toggle this to true if you want to test crash reporting in your app.
    if (kDebugMode) {
      // Crashlytics does not support web for now. See two other kIsWeb too.
      if (!kIsWeb) {
        await FirebaseCrashlytics.instance
            .setCrashlyticsCollectionEnabled(false);
      }
    }

    if (!kIsWeb) {
      // Pass all uncaught errors from the framework to Crashlytics.
      FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterError;
    }
    isFirebaseInit = true;
  } on Exception catch (exception, stack) {
    errorMessage += "'${exception.toString()}'\n\nStack:\n${stack.toString()}";
  } catch (error, stack) {
    errorMessage += "'${error.toString()}'\n\nStack:\n${stack.toString()}";
  }

  if (!isFirebaseInit) {
    runApp(
      Container(
        color: const Color(0xff008587),
        child: Center(
          child: Text('''Es tut uns Leid!

Es ist ein Fehler aufgetreten. Wenden Sie sich bitte an .

$errorMessage''', textScaleFactor: 0.8, textDirection: TextDirection.ltr),
        ),
      ),
    );
  } else {
    runZonedGuarded(() {
      WidgetsFlutterBinding.ensureInitialized();
      runApp(
        MultiProvider(
          providers: [
            ChangeNotifierProvider.value(
              value: services.get<ConnectivityService>(),
            ),
            ChangeNotifierProvider.value(
              value: services.get<AppClock>(),
            ),
            ChangeNotifierProvider.value(
              value: services.get<UserProvider>(),
            ),
            ChangeNotifierProvider.value(
              value: services.get<TherapyProvider>(),
            ),
            ChangeNotifierProvider.value(
              value: services.get<AuthProvider>(),
            ),
            ChangeNotifierProvider.value(
              value: services.get<TherapistProvider>(),
            ),
            ChangeNotifierProvider.value(
              value: services.get<LocalAuthProvider>(),
            ),
          ],
          child: MyApp(),
        ),
      );
    }, (Object error, StackTrace stackTrace) {
      if (!kIsWeb) {
        FirebaseCrashlytics.instance.recordError(error, stackTrace);
      }
    });
  }
}

class MyApp extends StatelessWidget {
  MyApp({Key key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Consumer3<UserProvider, AppClock, ConnectivityService>(
      builder: (context, userProvider, clock, connectivityService, _) {
        if (connectivityService.connectivityResult == ConnectivityResult.none) {
          return MaterialApp(
            localizationsDelegates: [
              S.delegate,
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate,
            ],
            supportedLocales: S.delegate.supportedLocales,
            home: InternetConnection(),
          );
        } else {
          return MaterialApp(
            title: '',
            theme: AppTheme.base,
            routes: Routes.routes,
            initialRoute: services.get<UserProvider>().guessInitialRoute(),
            localizationsDelegates: [
              S.delegate,
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate,
            ],
            supportedLocales: S.delegate.supportedLocales,
          );
        }
      },
    );
  }
}

System info

Issue occurs on: both Plugin name: connectivity_plus Plugin version: 2.0.1 Flutter doctor output:

[✓] Flutter (Channel stable, 2.5.2, on macOS 11.6 20G165 darwin-x64, locale de-DE) • Flutter version 2.5.2 at /Users/blackbox/Developing/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 3595343e20 (6 days ago), 2021-09-30 12:58:18 -0700 • Engine revision 6ac856380f • Dart version 2.14.3

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.1) • Android SDK at /Users/blackbox/Library/Android/sdk • Platform android-30, build-tools 30.0.1 • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165) • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS • Xcode at /Applications/Xcode.app/Contents/Developer • Xcode 13.0, Build version 13A233 • CocoaPods version 1.11.2

[✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2020.3) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)

[✓] VS Code (version 1.60.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.27.0

[✓] Connected device (3 available) • SM G955F (mobile) • ce0717174bdce4140c • android-arm64 • Android 9 (API 28) • H4x0r12 (mobile) • 00008101-000961681110001E • ios • iOS 15.0.1 19A348 • Chrome (web) • chrome • web-javascript • Google Chrome 94.0.4606.71 ! Error: H4x0r12 is busy: Making the device ready for development. Xcode will continue when H4x0r12 is finished. (code -10)

• No issues found!

github-actions[bot] commented 2 years ago

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 15 days