flutterdartflutter-dependenciesdart-null-safetynull-safety

The body might complete normally, causing 'null' to be returned, but the return type, 'Future<bool>', is a potentially non-nullable type


I upgraded my project to null-safety as they describe here dart migrate. I applied all suggestions but I get an error on one of my screens. How can I fix this?

  Future<bool> systemBackButtonPressed() {
    if (navigatorKeys[profileController.selectedIndex.value]!
        .currentState!
        .canPop()) {
      navigatorKeys[profileController.selectedIndex.value]!.currentState!.pop(
          navigatorKeys[profileController.selectedIndex.value]!.currentContext);
    } else {
      SystemChannels.platform.invokeMethod<void>('SystemNavigator.pop');
    }
  }

Usage is here :

Widget build(BuildContext context) {
    print(":}");
    return Obx(
          () => AnimatedContainer(
        height: Get.height,
        width: Get.width,
        curve: Curves.bounceOut,
        decoration: BoxDecoration(
          boxShadow: [
            BoxShadow(
              color: Color(0xff40000000),
              spreadRadius: 0.5,
              blurRadius: 20,
            ),
          ],
        ),
        transform: Matrix4.translationValues(
          drawerOpen.xOffset!.value,
          drawerOpen.yOffset!.value,
          0,
        )..scale(drawerOpen.scaleFactor!.value),
        duration: Duration(
          milliseconds: 250,
        ),
        child: WillPopScope(
          onWillPop: systemBackButtonPressed,
          child: InkWell(
            onTap: () {
              drawerOpen.xOffset!.value = 0;
              drawerOpen.yOffset!.value = 0;
              drawerOpen.scaleFactor!.value = 1.0;
              drawerOpen.isChange(false);
            },
            child: Scaffold(
              backgroundColor: pageBackGroundC,
              resizeToAvoidBottomInset: false,
              body: GetBuilder<ProfileController>(
                init: ProfileController(),
                builder: (s) => IndexedStack(
                  index: s.selectedIndex.value,
                  children: <Widget>[
                    // LogInScreen(),
                    NavigatorPage(
                      child: WatchListScreen(),
                      title: "Borsa",
                      navigatorKey: navigatorKeys[0],
                    ),
                    NavigatorPage(
                      child: OrderScreen(),
                      title: "Halka Arz",
                      navigatorKey: navigatorKeys[1],
                    ),
                    NavigatorPage(
                      child: PortFolioScreen(),
                      title: "Sermaye Artırımları",
                      navigatorKey: navigatorKeys[2],
                    ),
                    NavigatorPage(
                      child: FundScreen(),
                      title: "Haberler",
                      navigatorKey: navigatorKeys[3],
                    ),
                    NavigatorPage(
                      child: AccountScreen(),
                      title: "Hesabım",
                      navigatorKey: navigatorKeys[4],
                    ),
                  ],
                ),
              ),
              bottomNavigationBar: SuperFaBottomNavigationBar(),
            ),
          ),
        ),
      ),
    );
  }

Solution

  • Your systemBackButtonPressed() doesn't return anything, and it should return a Future of a bool. You can achieve returning the future if you mark it as async, and to return a bool just add the return in the end:

      Future<bool> systemBackButtonPressed() async {
        if (navigatorKeys[profileController.selectedIndex.value]!
        .currentState!
        .canPop()) {
       navigatorKeys[profileController.selectedIndex.value]!.currentState!.pop(
         navigatorKeys[profileController.selectedIndex.value]!.currentContext);
      } else {
        SystemChannels.platform.invokeMethod<void>('SystemNavigator.pop');
      }
      return true;
    }