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
2
Answers
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 studentYour code needs a lot of refactoring but you can simplify this color checking by use of Ternary operator
Your isPassed() is expecting a Student object. Remove the .pass from :
so that it only becomes :