简体   繁体   中英

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(),
            ),
          ),
        ),
      ),
    );
  }

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;
}

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question The body might complete normally, causing 'null' to be returned, but the return type, 'Future<DateTime*>*', is a potentially non-nullable type Error : The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type Flutter Error: The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type The body might complete normally, causing 'null' to be returned, but the return type, 'State<StatefulWidget>', is a potentially non-nullable type error: The body might complete normally, causing 'null' to be returned, but the return type, 'Widget', is a potentially non-nullable type The body might complete normally, causing 'null' to be returned, but the return type, 'FutureOr<DataModel>', is a potentially non-nullable type Flutter The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type StreamBuilder The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type Flutter The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM