gpt4 book ai didi

android - 使用 pushNamed() 从屏幕错误中 flutter 返回数据

转载 作者:IT老高 更新时间:2023-10-28 12:41:32 36 4
gpt4 key购买 nike

我使用 pushNamed() 从屏幕获取返回数据,如下所示:

_pushToPage2() async {
String result = await Navigator.pushNamed(context, "/page2");
print("$tag $result");
}

但是代码有问题,这是完整的错误堆栈跟踪:

E/flutter: [ERROR:flutter/shell/common/shell.cc(181)] Dart Error: Unhandled exception:
type 'MaterialPageRoute' is not a subtype of type 'Route'
0 NavigatorState._routeNamed (package:flutter/src/widgets/navigator.dart:1408:14)
1 NavigatorState.pushNamed (package:flutter/src/widgets/navigator.dart:1454:20)
2 _MyHomePageState._pushToPage2 (file:///Users/wang/flutter_project/flutter_navigation/lib/main.dart:113:50)
3 _MyHomePageState.build. (file:///Users/wang/flutter_project/flutter_navigation/lib/main.dart:58:19)
4 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:507:14)
5 _InkResponseState.build. (package:flutter/src/material/ink_well.dart:562:30)
6 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
7 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:242:9)
8 TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:175:7)
9 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:315:9)
10 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73:12)
11 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:11)
12 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:143:19)
13 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:121:22)
14 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:101:7)
15 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:64:7)
16 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:48:7)
17 _invoke1 (dart:ui/hooks.dart:153:13)
18 _dispatchPointerDataPacket (dart:ui/hooks.dart:107:5)

但以下代码运行正常:

_pushToPage2() async {
String result = await Navigator.push(
context, MaterialPageRoute(builder: (context) => Page2()));
print("$tag $result");
}

最佳答案

对我来说,错误是另一个错误 (type 'MaterialPageRoute<dynamic>' is not a subtype of type 'Route<String>'),我通过将返回值声明为动态类型来解决它:

dynamic result = await Navigator.of(context).pushNamed("/page");

虽然这会失去一些类型安全性,但它对我来说很好。

关于android - 使用 pushNamed() 从屏幕错误中 flutter 返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53077273/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com