Flutter路由传值无法正确传送的问题
最近在开发一个网络工具APP里面包括SSH terminal,在传值的时候发现无法显示传值,后来修改代码在传值的前面加上widget居然就可以了。把这个经验分享给大家吧!
传出页面:
Navigator.of(context)
.push(MaterialPageRoute(builder: (context) {
return Xterm_TerminalXX(
host: 'ssh://192.168.1.196:22',
username: 'root',
password: '********');
}));
传入页面:
class Xterm_TerminalXX extends StatefulWidget {
final String host;
final String username;
final String password;
Xterm_TerminalXX({this.host = '', this.username = '', this.password = ''});
@override
State<Xterm_TerminalXX> createState() => _Xterm_TerminalXXState();
}
class _Xterm_TerminalXXState extends State<Xterm_TerminalXX> {
@override
Widget build(BuildContext context) {
// dynamic argumentsData = ModalRoute.of(context)?.settings.arguments;
// host = argumentsData.host;
// username = argumentsData.username;
// password = argumentsData.password;
return MaterialApp(
title: 'Test route' + widget.host.toString(),
home: Scaffold(
body: Column(
children: [
Text('host:' + widget.host.toString()),
Text('username:' + widget.username.toString()),
Text('password:' + widget.password.toString()),
],
)
//Xterm_TerminalX(),
));
}
}