我在flutter中遇到错误(从first_page导航到second_page)

c90pui9n  于 2023-06-24  发布在  Flutter
关注(0)|答案(1)|浏览(124)

我有一个错误,从第一页导航到第二页,这是我的代码:

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: Text('Attendance List'),
      ),
      body: 
      Column(children: [
        FloatingActionButton(
  onPressed: () {
    Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => second_page()),
    );
  },
  child: Icon(Icons.add),
),
        
      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');
              }
            },
          );
        },
      ),
      ],
           
 
            ),
    );
  }
}

..........................................................................错误是(未为类型'_AttendanceListPageState'定义方法'second_page'。尝试将名称更正为现有方法的名称)如何修复它?]\

q1qsirdb

q1qsirdb1#

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: Text('Attendance List'),
      ),
      body: Column(
        children: [
          FloatingActionButton(
            onPressed: () {
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => SecondPage()),
              );
            },
            child: Icon(Icons.add),
          ),
          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类。

相关问题