flutter:: 是否可以将 statefulwidget 作为第二页加载?

flutter:: Is it possible to load statefulwidget as a second page?

我有 main.dart、a.dart 和 b.dart。 当我运行main.dart时,a和b的结果都在一页显示。但是我想让a和b的结果显示在不同的页面上。 所以我使用 MaterialPageRoute 在第一页上显示 a,在第二页上显示 b。 但是发生了错误。

The named parameter 'records' is required, but there's no corresponding argument.

我的猜测是a和b都是用statefulwidget写的,所以出现这个错误。 对此有何建议?

a.dart 例子

class Recorder extends StatefulWidget {
  final Function save;

  const Recorder({Key? key, required this.save}) : super(key: key);
  @override
  _RecorderState createState() => _RecorderState();
}

class _RecorderState extends State<Recorder> {

b.dart 例子

class Records extends StatefulWidget {
  final List<String> records;
  const Records({
    Key? key,
    required this.records,
  }) : super(key: key);

  @override
  _RecordsState createState() => _RecordsState();
}

class _RecordsState extends State<Records> {

main.dart 例子

class HomePage extends StatefulWidget {
  final String _appTitle;

  const HomePage({Key? key, required String title})
      : assert(title != null),
        _appTitle = title,
        super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

main.dart 例子(错误)

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: (){
        },
        child:  InkWell(child: Icon(Icons.mic),onTap: (){
          Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => Records());
              },),
      ),

您缺少记录参数。您的解决方案是删除

required this.records

或者将你的Navigator.push改成这个

 MaterialPageRoute(builder: (context) => Records(['']));