我正在尝试创建一个简单的 Flutter 项目以供学习。我需要删除选定的学生 -Photo- 但一开始我分配了 StudentList 私有所以当我尝试删除时 setState() 函数不起作用。如何将 StudentList 指定为全局。
这是我一开始写的代码:
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];
});
},
);
})),
我想这样分配 StudentList:
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
];
但这会抛出“无法在初始化程序中访问实例成员“...”。”错误。有没有其他方法可以全局分配StudentList,错误原因是什么?
如有必要,请提供完整版本的代码:
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);
}
}