我將我的專案升級為 null-safety,正如他們在此處描述的那樣dart migrate。我應用了所有建議,但在我的一個螢屏上出現錯誤。我怎樣才能解決這個問題?
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');
}
}
用法在這里:
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(),
),
),
),
),
);
}
uj5u.com熱心網友回復:
你systemBackButtonPressed()沒有回傳任何東西,它應該回傳一個布爾的未來。如果將其標記為 ,則可以實作回傳未來async,并且要回傳布林值,只需在最后添加回傳:
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;
}
uj5u.com熱心網友回復:
編輯:像這樣在函式名稱后添加 async 關鍵字Future<bool> systemBackButtonPressed() async {...
Future<bool>指定函式回傳一個布林值,因此它不能回傳空值。但是你沒有在你的函式中使用 return 陳述句,所以基本上它回傳 null。因此,您會收到此錯誤。要解決這個問題,只需將 return 陳述句添加到您的函式或將Future<bool>轉換為Future<void>
例子:
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.
}
}
如果您不想使用 return 陳述句,只需將布爾回傳未來函式轉換為 void 回傳未來函式。
而不是
Future<bool> systemBackButtonPressed()
使用
Future<void> systemBackButtonPressed()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/471868.html
