我正在尝试将数据从页面发送到flutter
项目中的另一个页面,尝试了在另一个问题中找到的所有方法,但是所有方法都失败了,这是我的代码:
第一页:
Navigator.pushNamed(context, '/line_details', arguments: {'line':line,});
第二页:
class _LineDetailsState extends State<LineDetails> {
Map data = {};
@override
Widget build(BuildContext context) {
data = ModalRoute.of(context).settings.arguments;
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.deepPurple,
title: Text("$data"),
),
);
}
}
注意:
line
是创建的自定义对象。错误:始终返回null,即使试图将单个字符串作为
{"test": "test string"}
发送,它也返回null
最佳答案
您的示例工作正常
MaterialApp中的路线
routes: { "/line_details": (context) => LineDetails(), },
按下FlatButton:
onPressed: () => Navigator.pushNamed(context, '/line_details', arguments: {'line':'test',}),
LineDetails类
class LineDetails extends StatefulWidget {
@override
_LineDetailsState createState() => _LineDetailsState();
}
class _LineDetailsState extends State<LineDetails> {
Map data = {};
@override
Widget build(BuildContext context) {
data = ModalRoute.of(context).settings.arguments;
print(data);
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.deepPurple,
title: Text("$data"),
),
);
}
}
与GestureDetector一起工作
new GestureDetector(
onTap: () => Navigator.pushNamed(context, '/line_details', arguments: {'line':'test',}),
child: new Container(child: new Text("GestureDetector"),),
),
关于android - 如何在 flutter 的 View 之间发送和接收参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61206847/