Flutter Listview.builder,RangeError索引不在0..1范围内,包括2:如何解决?

时间:2019-04-02 17:03:41

标签: listview dart flutter

我所有的“项目单元格”中都有一个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),),
                  ),
                      );
                      }
                    );
                   }
             ),
                )

2 个答案:

答案 0 :(得分:0)

删除我认为可以使用的[index]。

itemCount:snapshot.data [index] .projectMembers.length,

itemCount:snapshot.data [index] .projectMembers.length,

答案 1 :(得分:0)

发生这种情况是因为您使用的是相同的变量index,但是scopes的集合有所不同,所以这里有4个项目:

  

itemCount:snapshot.data [index] .projectMembers.length,

但是以后使用时

  

Text(snapshot.data [index] .projectMembers [index] .username [0],style:TextStyle(color:Colors.white),),

对于用户名只有1个[william],它将永远不存在索引2,因此我认为您可以执行类似this example的操作

在内部,您可以创建一个文本小部件,然后使用胖箭头功能或按索引的匿名功能在其中添加文本小部件,如所示文档中的示例。