flutter - 如何导航到具有构造函数参数的页面?

标签 flutter dart

我的页面有状态。这里是:

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/

相关文章:

image - 如何在 Flutter 中分割/分割图像

flutter - 如何在 TextSpan 中 backrgroundColor full?

android - MissingPluginException(在 channel plugins.flutter.io/shared_preferences上没有找到方法getAll的实现)-现有解决方案不起作用-

firebase - 使用 rxDart 合并 Firestore 流

dart - Dart 列表元素中的空值

flutter - 显示键盘时如何拦截 flutter 后退按钮

svg - 如何在 google_maps_flutter Flutter 插件中使用 SVG 标记?

dart - Flutter/如何在Statefulwidget之间传递和获取数据

c# - 是否可以使用 dart 将 linq lambda 表达式转换为 SQL?

routes - Action 后生成 flutter 路线?