参数类型’int’不能分配给参数类型student

huangapple go评论67阅读模式
英文:

The argument type 'int' can't be assigned to the parameter type student

问题

在这个程序中,我想要在学生通过时显示黄色或琥珀色,如果学生失败则显示红色,但出现了错误,错误信息是“参数类型'int'无法分配给参数类型'student'”。

以下是相关代码的翻译:

backgroundColor: isPassed(this.studentsList[index].pass),
child: getIcon(this.studentsList[index].pass))

如果需要更多帮助,请随时提出。您还可以查看您提供的GitHub链接以获取完整的代码。

英文:

in this program i want to make yellow or amber color appears if the the student is passed and red if the student is failed but it make an error The argument type 'int' can't be assigned to the parameter type student

    import 'package:flutter/material.dart';
import 'package:students/student_detail.dart';
import 'dart:async';
import 'package:students/models/student.dart';
import 'package:students/utilities/sql_helper.dart';
import 'package:sqflite/sqflite.dart';

class StudentsList extends StatefulWidget {
  const StudentsList({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return StudentsState();
  }
}

class StudentsState extends State<StudentsList> {
  SQL_Helper helper = new SQL_Helper();
  List<Student> studentsList = [
    Student('mohammed ', ' lorim posdafp0o jsdpof posjkf sd f', 1,
        '"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua'),
    Student('Sadd ', ' lorim posdafp0o jsdpof posjkf sd f', 2,
        '"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua'),
    Student('mmed ', ' lorim posdafp0o jsdpof posjkf sd f', 1,
        '"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua'),
    Student('mopd ', ' lorim posdafp0o jsdpof posjkf sd f', 1,
        '"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua'),
    Student('ohamd ', ' lorim posdafp0o jsdpof posjkf sd f', 1,
        '"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua'),
    Student('posdafp0o ', ' lorim posdafp0o jsdpof posjkf sd f', 1,
        '"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua'),
    Student('mohammed ', ' lorim posdafp0o jsdpof posjkf sd f', 2,
        '"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua'),
    Student('posd ', ' lorim posdafp0o jsdpof posjkf sd f', 1,
        '"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua'),
    Student('afp0o ', ' lorim posdafp0o jsdpof posjkf sd f', 2,
        '"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua'),
    Student('Mojahed ', ' lorim posdafp0o jsdpof posjkf sd f', 2,
        '"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua'),
    Student('Ahmed ', ' lorim posdafp0o jsdpof posjkf sd f', 1,
        '"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua'),
    Student('Amani ', ' lorim posdafp0o jsdpof posjkf sd f', 1,
        '"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua'),
  ];
  int count = 0;
  void navigate(String apptitle) {
    Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) => StudentDetail(apptitle),
        ));
  }

  @override
  Widget build(BuildContext context) {
    if (studentsList == null) {
      var studentsList = <List<Student>>[];
      updateListView();
    }

    return Scaffold(
        appBar: AppBar(
          title: const Text("Students"),
        ),
        body: getStudentsList(), //يعرف ليست اسمها جيت ستيودنت
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            navigate("Add a student");
          },
          tooltip: 'Add Student',
          child: const Icon(Icons.add),
        ));
  }

  ListView getStudentsList() {
    return ListView.builder(
        itemCount: studentsList.length,
        itemBuilder: (BuildContext context, int index) {
          return Card(
            color: Colors.white,
            elevation: 2.0,
            child: ListTile(
              leading: CircleAvatar(
                  backgroundColor: isPassed(studentsList[index].pass),
                  child: getIcon(studentsList[index].pass)),
              title: Text(this.studentsList[index].name),
              subtitle: Text(this.studentsList[index].description +
                  " | " +
                  this.studentsList[index].date),
              trailing: const Icon(
                Icons.close,
                color: Colors.grey,
              ),
              onTap: () {
                _delete(context, this.studentsList[index]);
              },
            ),
          );
        });
  }

  Color isPassed(Student student) {
    switch (student.pass) {
      case 1:
        return Colors.amber;
        break;
      case 2:
        return Colors.red;
        break;
      default:
        return Colors.amber;
    }
  }

  Icon getIcon(Student student) {
    switch (student.pass) {
      case 1:
        return Icon(Icons.check);
        break;
      case 2:
        return Icon(Icons.close);
        break;
      default:
        return Icon(Icons.check);
    }
  }

  void _delete(BuildContext context, Student student) async {
    int ressult = await helper.deleteStudent(student.id);
    if (ressult != 0) {
      _showSenckBar(context, "Student has been deleted");
      updateListView();
    }
  }

  void _showSenckBar(BuildContext context, String msg) {
    final snackBar = SnackBar(
      content: Text(msg),
    );
    ScaffoldMessenger.of(context).showSnackBar(snackBar);
  }

  void updateListView() {
    final Future<Database> db = helper.initializedDatabase();
    db.then((database) {
      Future<List<Student>> students = helper.getStudentList();
      students.then((theList) {
        setState(() {
          this.studentsList = theList;
          this.count = theList.length;
        });
      });
    });
  }
}

and the error in this lines of code

backgroundColor: isPassed(this.studentsList[index].pass),
              child: getIcon(this.studentsList[index].pass))

there is the entire code on github link
https://github.com/abdelrahman992-cpu/intstudent

答案1

得分: 2

这是因为您的方法 isPassed() 期望一个 Student 实例,而不是一个 student.pass 属性。

您可以使用 isPassed(studentsList[index]) 调用该方法,或者更改方法的参数签名,使其接受 int 而不是 student

Color isPassed(int pass) {
  switch (pass) {
    case 1:
      return Colors.amber;
      break;
    case 2:
      return Colors.red;
      break;
    default:
      return Colors.amber;
  }
}

您的代码需要进行大量重构,但您可以使用三元运算符来简化颜色检查:

backgroundColor: studentsList[index].pass == 2 ? Colors.red : Colors.amber
英文:

This is because your method isPassed() is expecting Student instance. Not a student.pass property.

You can either invoke method with isPassed(studentsList[index] or change method's signature to receive int instead of student

 Color isPassed(int pass) {
    switch (pass) {
      case 1:
        return Colors.amber;
        break;
      case 2:
        return Colors.red;
        break;
      default:
        return Colors.amber;
    }
  }

Your code needs a lot of refactoring but you can simplify this color checking by use of Ternary operator

 backgroundColor: studentsList[index].pass == 2 ? Colors.red : Colors.amber

答案2

得分: 0

你的 isPassed() 函数期望一个 Student 对象。从以下代码中移除 .pass 部分:

backgroundColor: isPassed(studentsList[index].pass),

这样它就变成:

backgroundColor: isPassed(studentsList[index]),
英文:

Your isPassed() is expecting a Student object. Remove the .pass from :

backgroundColor: isPassed(studentsList[index].pass),

so that it only becomes :

backgroundColor: isPassed(studentsList[index]),

huangapple
  • 本文由 发表于 2023年1月9日 04:54:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051218.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定