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(),
),
),
),
),
);
}
CodePudding user response:
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;
}
CodePudding user response:
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()