我的页面有状态。这里是:
class TestPage extends StatefulWidget {
static const String id = 'TestPage';
final String testString;
TestPage(this.testString);
@override
_TestPageState createState() => _TestPageState();
}
class _TestPageState extends State<TestPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(child: Text('Hello ${widget.testString}'))
);
}
}
该页面具有一个构造函数,该构造函数将字符串作为默认值。
final String testString;
从另一个页面,我打电话到该页面。我想打开它并给它或传递给它一个String值:
Navigator.pushNamed(context, TestPage(myString));
但是,它告诉我:
不能将参数类型“TestPage”分配给参数类型String。
我究竟做错了什么?这不是实例化此类并使其出现的正确方法吗?
谢谢
最佳答案
试试这个
Navigator.push( context, MaterialPageRoute( builder: (context) => TestPage(testString: 'Hello',), ));
关于flutter - 如何导航到具有构造函数参数的页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57809561/