Search code examples
flutterstatefulwidgetmaterialpageroute

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


I have main.dart, a.dart and b.dart. When I run main.dart, the results of a and b are all displayed in one page. But I want the results of a and b to be displayed on different pages. So I used MaterialPageRoute to make a visible on the first page and b on the second page. but An error has occurred.

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

My guess is that a and b are both written with statefulwidget, so the error appears. Any advice on this?

a.dart example

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 example

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 example

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 example(error)

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

Solution

  • You are missing your parameter for Records. Your solution is to either remove

    required this.records

    or change your Navigator.pushto this

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