0

I have this problem with navigation, when ever I press this card I get this error Another exception was thrown: Null check operator used on a null value in debug console. Whenever I press cards if not logged in I must navigate to UrlScreen

 void tapped(int index) {
      if (index == 0) {
        bool isBaseUrlEntered = StoreDetails.checkBaseUrlEnteredSession(
            StoreDetails.isBaseUrlEntered);
        print(isBaseUrlEntered);
        if (isBaseUrlEntered) {
          Navigator.pushReplacement(
            context,
            MaterialPageRoute(
                builder: (context) => SplashScreen(nameType: "Pre Sales")),
          );
        } else {
          Navigator.pushReplacement(
            context,
            MaterialPageRoute(
                builder: (context) => UrlScreen(nameType: "Pre Sales")),
          );
        }
      } else if (index == 3) {
        bool isProjectBaseUrlEntered =
            ProjectMgmtStoreDetails.checkProjectBaseUrlEnteredSession(
                ProjectMgmtStoreDetails.isProjectBaseUrlEntered);
        print(isProjectBaseUrlEntered);
        if (isProjectBaseUrlEntered) {
          Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) => SplashScreen(nameType: "Project Mgmt")),
          );
        } else {
          Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) => UrlScreen(nameType: "Project Mgmt")),
          );
        }

And the full error in debug console is

The following _CastError was thrown while handling a gesture:
Null check operator used on a null value

When the exception was thrown, this was the stack:
#0      ProjectMgmtStoreDetails.checkProjectMgmtLoginSession (package:bhoomi/utils/store_details_project_mgmt.dart:43:27)
#1      WelcomeScreenState.build.tapped (package:bhoomi/screens/welcome_screens/welcome_screen.dart:113:49)
#2      WelcomeScreenState.build.<anonymous closure>.<anonymous closure>.<anonymous closure>
(package:bhoomi/screens/welcome_screens/welcome_screen.dart:366:48)
#3      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:198:24)
#4      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:608:11)
#5      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:296:5)
#6      BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:267:7)
#7      GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:157:27)
#8      GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:443:20)
#9      GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:419:22)
#10     RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:322:11)
#11     GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:374:7)
#12     GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:338:5)
#13     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:296:7)
#14     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:279:7)
#18     _invoke1 (dart:ui/hooks.dart:170:10)
#19     PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:331:7)
#20     _dispatchPointerDataPacket (dart:ui/hooks.dart:94:31)
(elided 3 frames from dart:async)

Handler: "onTap"
Recognizer:
  TapGestureRecognizer#1a171

Edit base on Grekkq suggestion

class ProjectMgmtStoreDetails {
  static const isProjectMgmtLoggedIn = 'isProjectMgmtLoggedIn';
  static const isProjectBaseUrlEntered = 'isProjectBaseUrlEntered';
  static const projectMgmt = 'projectMgmt';
  static const name = 'name';
  static const projectNameList = 'projectNameList';
  static const valuationList = 'valuationList';
  static const projectAuthUrl = 'projectAuthUrl';
  static const projectBaseUrl = 'projectBaseUrl';
  static const title = 'title';
  static const userId = 'userid';
  static const typeList = 'typeList';
  static const verbList = 'verbList';
  static const staffName = 'staffName';
  static const staffId = 'staffId';
  static const notificationDetailList = 'notificationDetailList';
  static const fromDateFilter = 'fromDateFilter';
  static const toDateFilter = 'toDateFilter';
  static const valuationFilter = 'valuationFilter';
  static const valuationIdFilter = 'valuationIdFilter';
  static const projectId = 'projectId';
  static const vendorName = 'vendorName';
  static const vendorId = 'vendorId';

  static Future<SharedPreferences?>? get _instance1 async => _prefsInstance1!;
  static SharedPreferences? _prefsInstance1;

  static Future<SharedPreferences?>? init() async {
    _prefsInstance1 = await _instance1;
    return _prefsInstance1!;
  }

  // for project mgmt credentials
  static bool? checkProjectMgmtLoginSession(String key) {
    return _prefsInstance1?.getBool(key);
  }

  static Future<bool> createProjectMgmtLoginSession() async {
    var prefs = await _instance1;
    return prefs!.setBool(isProjectMgmtLoggedIn, true);
  }

  //for base url session
  static bool checkProjectBaseUrlEnteredSession(String key) {
    return _prefsInstance1!.getBool(key) ?? false;
  }

  static Future<bool> createProjectBaseUrlEnteredSession() async {
    var prefs = await _instance1;
    return prefs!.setBool(isProjectBaseUrlEntered, true);
  }

  // for project mgmt
  static Future<bool> setProjectMgmtToken(String value) async {
    var prefs = await _instance1;
    return prefs!.setString(projectMgmt, value);
  }

  static String getProjectMgmtToken(String key) {
    return _prefsInstance1!.getString(key) ?? "";
  }

  static Future<bool> setLoggedInUserName(String value) async {
    var prefs = await _instance1;
    return prefs!.setString(name, value);
  }

  static String getLoggedInUserName(String key) {
    return _prefsInstance1!.getString(key) ?? "";
  }

so on
  • You should add content of the `store_details_project_mgmt.dart`. Also why you pass to the `checkProjectBaseUrlEnteredSession` something that is already inside this class? – Grekkq May 06 '22 at 06:38

1 Answers1

0

The Null check operator used on a null value exception message usually occurs when you use ! operator on a nullable instance which wasn't initialized. You can refer to this answer for further explanation.

Looking at the stack you can see that probably the ProjectMgmtStoreDetails class is problematic. If you are using modern IDE you should be able to CTRL+Click on the line number in you exception message and it should take you where the exception occurred.

I would also suggest to not store any authentication/other secrets details in your app code, you can refer to this for potential solution.

Grekkq
  • 679
  • 6
  • 14