You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter页面跳转报错:second_page方法未定义的解决方法

修复Flutter页面跳转错误:"The method 'second_page' isn't defined..."

错误原因

你遇到的错误核心是Dart类名遵循大驼峰命名规范,你在跳转时写的second_page()是小写下划线形式,会被编译器识别为方法而非页面类,而你的_AttendanceListPageState中并没有定义这个方法。

修复步骤

1. 修正页面类调用

在FloatingActionButton的点击回调里,把second_page()改成SecondPage()——这是因为你导入的second_page.dart中,页面类的命名必然是符合Dart规范的大驼峰形式(比如class SecondPage extends StatelessWidget)。

2. 修复Column与ListView的布局冲突

你的代码直接在Column中嵌套ListView会触发布局报错(ListView会试图占用无限高度),需要用Expanded组件包裹ListView,让它占据Column剩余的可用空间。

修改后的完整代码

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:attendance_app/database_helper.dart';
import 'package:attendance_app/student.dart';
import 'second_page.dart';

class AttendanceListPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _AttendanceListPageState();
  }
}

class _AttendanceListPageState extends State<AttendanceListPage> {
  List<Student> _students = [];

  @override
  void initState() {
    super.initState();
    _loadStudents();
  }

  _loadStudents() async {
    List<Map<String, dynamic>> rows = await DatabaseHelper.instance.queryAllRows();
    List<Student> students = [];
    for (var row in rows) {
      students.add(Student.fromMap(row));
    }
    setState(() {
      _students = students;
    });
  }

  _updateAttendanceStatus(int id, String status) async {
    await DatabaseHelper.instance.updateAttendanceStatus(id, status);
    _loadStudents();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Attendance List'),
      ),
      body: Column(
        children: [
          FloatingActionButton(
            onPressed: () {
              Navigator.push(
                context,
                // 修复:将second_page()改为SecondPage()
                MaterialPageRoute(builder: (context) => SecondPage()),
              );
            },
            child: const Icon(Icons.add),
          ),
          // 修复:用Expanded包裹ListView,解决布局高度冲突
          Expanded(
            child: ListView.builder(
              itemCount: _students.length,
              itemBuilder: (BuildContext context, int index) {
                return CheckboxListTile(
                  title: Text(_students[index].name),
                  subtitle: Text(_students[index].rollNo),
                  value: _students[index].status == 'present',
                  onChanged: (bool? value) {
                    if (value == true) {
                      _updateAttendanceStatus(_students[index].id, 'present');
                    } else {
                      _updateAttendanceStatus(_students[index].id, 'absent');
                    }
                  },
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}

额外验证

确保你的second_page.dart文件中,页面类命名确实为SecondPage,示例结构如下:

// second_page.dart示例
import 'package:flutter/material.dart';

class SecondPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Second Page')),
      body: const Center(child: Text('This is the second page')),
    );
  }
}

内容的提问来源于stack exchange,提问作者asdo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 12:02:50