我所有的“项目单元格”中都有一个listview.builder。我从类列表中获取了索引,这给了我一个错误:
引发了另一个异常:RangeError(索引):无效值:不在
范围0..1(含):2。
对于输入字符串:“1,277”
对于输入字符串:“1,277”
对于输入字符串:“1,277”
对于输入字符串:“1,277”
对于输入字符串:“1,277”
对于输入字符串:“1,277”
...
class SchoolProject {
final int index;
final String name;
final String about;
final String subject;
final String daysLeft;
final List<User> projectMembers;
final List<SchoolProjectTask> tasks;
SchoolProject(this.index, this.name, this.about, this.subject,
this.daysLeft, this.projectMembers, this.tasks);
}
class User {
final int index;
final String username;
final String description;
final String school;
User(this.index, this.school, this.description, this.username);
}
var SchoolProjectOne = SchoolProject(
0,
"Historia Prov - Berlinmuren",
"Läs in på Berlinmuren och kubrakrisen, hur usa mot soviet
skapade"
"kalla kriget och hur kriget aldrig bröt ut för att folk var
rädda!",
"History",
"2 Days",
[William],
[
SchoolProjectTask(
0,
"Läs Om Berlinmuren",
"",
"2 Days",
[William]
),
SchoolProjectTask(
1,
"Ta reda på Kubakrisen",
"",
"2 Days",
[William]
),
SchoolProjectTask(
2,
"Kommunism vs Capitalism",
"",
"2 Days",
[William]
),
]
);
Future<List<SchoolProject>> _getSchoolProject() async {
var data = allSchoolProjects;
print(data);
return data;
}
Container(
height: 50,
margin: EdgeInsets.only(left: 10, right: 10, bottom: 12, top:5),
child: FutureBuilder(
future: _getSchoolProject(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if(snapshot.data == null) {
return Container(
alignment: Alignment.centerLeft,
padding: EdgeInsets.all(8),
child: CircularProgressIndicator(
strokeWidth: 2,
),
);
}
else return ListView.builder(
scrollDirection: Axis.horizontal,
padding: EdgeInsets.all(5),
itemCount:
snapshot.data[index].projectMembers.length,
itemBuilder: (BuildContext context, int index) {
return Container(
margin: EdgeInsets.only(right: 10),
child: CircleAvatar(
backgroundColor: Color(0xFFBA68C8),
child:
Text(snapshot.data[index].projectMembers[index].username[0],
style: TextStyle(color: Colors.white),),
),
);
}
);
}
),
)
最佳答案
删除我认为会起作用的[index]。
itemCount:snapshot.data [index] .projectMembers.length,
itemCount:snapshot.data [index] .projectMembers.length,
关于listview - Flutter Listview.builder,RangeError索引不在0..1(包括0)范围内。2.如何解决?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55480090/