gpt4 book ai didi

android - Flutter:如何使用Navigator PushNamed发送屏幕构造器所需的参数

转载 作者:行者123 更新时间:2023-12-01 21:48:45 25 4
gpt4 key购买 nike

我有一个路线图,从该路线图中我可以快速看到屏幕。我需要将三个参数传递给下一个屏幕的构造方法。我想使用Navigator.pushNamed(),但是当我使用arguments属性时,它似乎不起作用。

//Screen to build 
class ResultPage extends StatelessWidget{
ResultPage(
{@required this.calc1Result,
@required this.calc2Text,
@required this.interpretation});

final String calc1Result;
final String calc2Text;
final String interpretation;

@override
Widget build(BuildContext context) { //etc.. etc... }

--------------------------------------------------------------------------

然后,我需要从另一个页面调用此屏幕:
  //Call to ResultPage from another screen
Calculator calculator = Calculator(param1: param1, param2: param2);


Navigator.pushNamed(context, 'resultPage', arguments:
{
'calc1Result': calculator.getCalc1Result(),
'calc2Text': calculator.getCalc2Text()
'interpretation': calculator.getInterpretation(),
});

它根本不起作用,结果始终为Null。

如果我使用Navigator.push()做同样的事情,它将起作用。
//Working alternative - I don't want to use this:
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ResultPage(
calc1Result: calculator.getCalc1Result(),
calc2Text: calculator.getCalc2Text(),
interpretation: calculator.getInterpretation(),
),
),
);

最佳答案

您是否已将命名路线添加到MaterialApp小部件?

MaterialApp(
routes: {
ExtractArgumentsScreen.routeName: (context) => ExtractArgumentsScreen(),
},
);

看这里:
https://flutter.dev/docs/cookbook/navigation/navigate-with-arguments

然后在ResultPage中,您必须使用以下命令提取参数:
@override
Widget build(BuildContext context) {
// Extract the arguments from the current ModalRoute settings and cast
// them as ScreenArguments.
final args = ModalRoute.of(context).settings.arguments;

...
}

关于android - Flutter:如何使用Navigator PushNamed发送屏幕构造器所需的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61849279/

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