1

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(),
            ),
          ),
        ),
      ),
    );
  }
Taady
  • 2,409
  • 2
  • 10
  • 24
  • What is the error? – George Rabbat May 08 '22 at 18:51
  • "The body might complete normally, causing 'null' to be returned, but the return type, 'Future', is a potentially non-nullable type" is the error at first Future systemBackButtonPressed() { – Taady May 08 '22 at 19:19

2 Answers2

3

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;
}
galimpic
  • 46
  • 1
  • 2
1

Edit: Add async keyword after your function name like this Future<bool> systemBackButtonPressed() async {...

Future<bool> specifies that the function returns a boolean value, therefore it can't return null value. Yet you don't use return statements inside your function, so basically it returns null. Therefore you get this error. To solve this, simple add return statements to your functions or just convert Future<bool> to Future<void>

Example:

Future<bool> systemBackButtonPressed() {
    if (navigatorKeys[profileController.selectedIndex.value]!
        .currentState!
        .canPop()) {
      
      navigatorKeys[profileController.selectedIndex.value]!.currentState!.pop(
          navigatorKeys[profileController.selectedIndex.value]!.currentContext);
      return true; //You just need to add this line
    } else {
      SystemChannels.platform.invokeMethod<void>('SystemNavigator.pop');
      return false; //You need to add return conditions for all cases.
    }
  }

If you don't want to use return statements just convert your boolean returning future function to a void returning future function.

Instead of Future<bool> systemBackButtonPressed() Use Future<void> systemBackButtonPressed()

Novice Coder
  • 219
  • 3
  • 6
  • Thank you, but both of the 2 methods not working. If I add " return" as you described i get "error: A value of type 'bool' can't be returned from the method 'systemBackButtonPressed' because it has a return type of 'Future'." error – Taady May 08 '22 at 19:54
  • And if I convert to void i get this error "error: The argument type 'Future Function()' can't be assigned to the parameter type 'Future Function()?'. " – Taady May 08 '22 at 19:55
  • Oh I missed the fact that you forgot to add async keyword to your function – Novice Coder May 08 '22 at 19:58