I am trying to create a simple flutter project for learning. I need to delete the selected student -Photo- but at the beginning I assigned the StudentList private so setState() function is not working when I try to delete. How can I assign StudentList as global.
This is the code I wrote at the beginning:
class _MyAppState extends State<MyApp> {
StudentManager ChoosedStudent = new StudentManager.WithId(0, "", "", 0, "");
StudentManager student1 = new StudentManager.WithId(1, "Name1", "LastName1",
22, "https://uifaces.co/our-content/donated/hvaUVob5.jpg");
StudentManager student2 = new StudentManager.WithId(2, "Name2", "LastName2",
70, "https://uifaces.co/our-content/donated/eqUZLcBO.jpg");
StudentManager student3 = new StudentManager.WithId(3, "Name3", "LastName3",
55, "https://uifaces.co/our-content/donated/-oe25tWA.png");
StudentManager student4 = new StudentManager.WithId(
4,
"Name4",
"LastName4",
99,
"https://images.generated.photos/ukvWCGJJF8xoZ_rvVSFDLnQ-WDkGw2WsZ53uPPm63M8/rs:fit:512:512/Z3M6Ly9nZW5lcmF0/ZWQtcGhvdG9zLzA3/OTI0MTAuanBn.jpg");
StudentManager student5 = new StudentManager.WithId(
5, "Name5", "LastName5", 45, "https://thispersondoesnotexist.com/image");
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text("Student Exam Result System"),
),
body: bodyBuilder(context),
);
}
bodyBuilder(BuildContext context) {
List<StudentManager> StudentList = [
student1,
student2,
student3,
student4,
student5
];
return Column(
children: [
Expanded(
child: ListView.builder(
itemCount: StudentList.length,
itemBuilder: (BuildContext context, int index) {
if (StudentList[index].Grade >= 50) {
StudentList[index].IsPassed = true;
} else if (StudentList[index].Grade < 50) {
StudentList[index].IsPassed = false;
}
return ListTile(
leading: CircleAvatar(
backgroundImage:
NetworkImage(StudentList[index].PhotoURL),
),
title: Text(StudentList[index].Name +
" " +
StudentList[index].Surname),
subtitle: Text(
"${StudentList[index].Name} named students grade is "
"${StudentList[index].Grade}, ${StudentList[index].AlphebaticalGrade}"),
isThreeLine: true,
trailing: buildStatusIcon(StudentList[index].IsPassed),
onTap: () {
setState(() {
ChoosedStudent = StudentList[index];
});
},
);
})),
I want to assign StudentList like this:
class _MyAppState extends State<MyApp> {
StudentManager ChoosedStudent = new StudentManager.WithId(0, "", "", 0, "");
StudentManager student1 = new StudentManager.WithId(1, "Name1", "LastName1",
22, "https://uifaces.co/our-content/donated/hvaUVob5.jpg");
StudentManager student2 = new StudentManager.WithId(2, "Name2", "LastName2",
70, "https://uifaces.co/our-content/donated/eqUZLcBO.jpg");
StudentManager student3 = new StudentManager.WithId(3, "Name3", "LastName3",
55, "https://uifaces.co/our-content/donated/-oe25tWA.png");
StudentManager student4 = new StudentManager.WithId(
4,
"Name4",
"LastName4",
99,
"https://images.generated.photos/ukvWCGJJF8xoZ_rvVSFDLnQ-WDkGw2WsZ53uPPm63M8/rs:fit:512:512/Z3M6Ly9nZW5lcmF0/ZWQtcGhvdG9zLzA3/OTI0MTAuanBn.jpg");
StudentManager student5 = new StudentManager.WithId(
5, "Name5", "LastName5", 45, "https://thispersondoesnotexist.com/image");
List<StudentManager> StudentList = [
student1,
student2,
student3,
student4,
student5
];
but this is throwing "The instance member "..."' can't be accessed in an initializer." Error. is there any other way to assign StudentList globally and what is the reason of the error?
Full version of the code if it's neccesary:
import 'dart:ffi';
import 'package:by_myself/models/students.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(home: MyApp()));
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
StudentManager ChoosedStudent = new StudentManager.WithId(0, "", "", 0, "");
StudentManager student1 = new StudentManager.WithId(1, "Name1", "LastName1",
22, "https://uifaces.co/our-content/donated/hvaUVob5.jpg");
StudentManager student2 = new StudentManager.WithId(2, "Name2", "LastName2",
70, "https://uifaces.co/our-content/donated/eqUZLcBO.jpg");
StudentManager student3 = new StudentManager.WithId(3, "Name3", "LastName3",
55, "https://uifaces.co/our-content/donated/-oe25tWA.png");
StudentManager student4 = new StudentManager.WithId(
4,
"Name4",
"LastName4",
99,
"https://images.generated.photos/ukvWCGJJF8xoZ_rvVSFDLnQ-WDkGw2WsZ53uPPm63M8/rs:fit:512:512/Z3M6Ly9nZW5lcmF0/ZWQtcGhvdG9zLzA3/OTI0MTAuanBn.jpg");
StudentManager student5 = new StudentManager.WithId(
5, "Name5", "LastName5", 45, "https://thispersondoesnotexist.com/image");
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text("Student Exam Result System"),
),
body: bodyBuilder(context),
);
}
bodyBuilder(BuildContext context) {
List<StudentManager> StudentList = [
student1,
student2,
student3,
student4,
student5
];
return Column(
children: [
Expanded(
child: ListView.builder(
itemCount: StudentList.length,
itemBuilder: (BuildContext context, int index) {
if (StudentList[index].Grade >= 50) {
StudentList[index].IsPassed = true;
} else if (StudentList[index].Grade < 50) {
StudentList[index].IsPassed = false;
}
return ListTile(
leading: CircleAvatar(
backgroundImage:
NetworkImage(StudentList[index].PhotoURL),
),
title: Text(StudentList[index].Name +
" " +
StudentList[index].Surname),
subtitle: Text(
"${StudentList[index].Name} named students grade is "
"${StudentList[index].Grade}, ${StudentList[index].AlphebaticalGrade}"),
isThreeLine: true,
trailing: buildStatusIcon(StudentList[index].IsPassed),
onTap: () {
setState(() {
ChoosedStudent = StudentList[index];
});
},
);
})),
Text("Student: " + ChoosedStudent.Name),
Center(
child: Row(
children: [
Flexible(
fit: FlexFit.tight,
flex: 1,
child: RaisedButton(
onPressed: () {},
color: Colors.blueAccent,
textColor: Colors.white,
child: Row(
children: [
SizedBox(
width: 16.0,
),
Icon(Icons.add),
Text("Add"),
],
),
),
),
Flexible(
fit: FlexFit.tight,
flex: 1,
child: RaisedButton(
onPressed: () {},
color: Colors.blueAccent,
textColor: Colors.white,
child: Row(
children: [
SizedBox(
width: 16.0,
),
Icon(Icons.update),
Text("Update"),
],
),
),
), //KAY
Flexible(
fit: FlexFit.tight,
flex: 1,
child: RaisedButton(
onPressed: () {
setState(() {
StudentList.remove(ChoosedStudent);
print(StudentList);
});
},
color: Colors.blueAccent,
textColor: Colors.white,
child: Row(
children: [
SizedBox(
width: 16.0,
),
Icon(Icons.delete),
Text("Delete"),
],
),
),
),
],
),
),
],
);
}
}
Widget buildStatusIcon(bool IsPassed) {
if (IsPassed == true) {
return Icon(Icons.done);
} else if (IsPassed == false) {
return Icon(Icons.warning);
} else {
return Icon(Icons.error);
}
}