flutter :不支持的操作:无法添加到不可修改的列表

2024-03-04

我在 StatelessWidget 中有一个 ListView 。它有项目,每个项目都包含一个复选框。当有人检查某个项目时,我希望 ListView 将其作为参数发送到另一个页面。但是当我这样做时,它给了我这个错误:

I/flutter ( 7067): The following UnsupportedError was thrown while handling a gesture:
I/flutter ( 7067): Unsupported operation: Cannot add to an unmodifiable list
I/flutter ( 7067): When the exception was thrown, this was the stack:

这是我的代码

class StudentsList extends StatelessWidget {
  final List<Child> mList;
  StudentsList({this.mList});

  @override
  Widget build(BuildContext context) {
    List<Child> selectedList = [];

    return Container(
      margin: EdgeInsets.only(top: 50, bottom: 20),
      child: ListView.builder(
        shrinkWrap: true,
        physics: ClampingScrollPhysics(),
        itemCount: mList == null ? 0 : mList.length,
        padding: EdgeInsets.only(right: 10),
        itemBuilder: (BuildContext context, int position) {
          return GestureDetector(
            onTap: () {
              if (selectedList.isEmpty) {
                Navigator.push(
                  context,
                  new MaterialPageRoute(
                    builder: (BuildContext context) => SolokPage(
                      mChildList: [mList[position]],
                      isTeacher: true,
                    ),
                  ),
                );
              } else {
                if (!selectedList.contains(mList[position])) {
                  selectedList.add(mList[position]);
                }
                Navigator.push(
                  context,
                  new MaterialPageRoute(
                    builder: (BuildContext context) => SolokPage(
                      mChildList: selectedList,
                      isTeacher: true,
                    ),
                  ),
                );
              }
            },
            child: StudentItem(
              student: mList[position],
            ),
          );
        },
      ),
    );
  }
}

无状态 Widget 属性是不可变的

class StudentsList extends StatelessWidget {
  // final means, flutter will not change value in future
  final List<Child> mList; 
  StudentsList({this.mList});

Why ?

Because Flutter期望不存在任何业务逻辑在 StatelessWidget 中。 如果我们需要在学生列表中添加新的学生,则将其视为业务逻辑。 如果我们需要删除学生列表中的某个学生,这被认为是业务逻辑。

因此,通过使用无状态 widget,Flutter 将only重点关注它将如何在屏幕上显示、宽度是多少、约束等等。

这就是为什么我们发现final类属性之前的语法StatelessWidget.

和我们的大学生活很相似。我们的Grades最终报告中标注的这一点,即使我们大学毕业后也不会改变。正如它所说的那样总结报告,那么它一定是final.

有状态 Widget 属性是可变的

Why ?因为 flutter 期望业务逻辑驻留在在 StatefulWidget 中。

需要做出的改变

所以我建议更改 StudentsList Widget,从此:

class StudentsList extends StatelessWidget {
  final List<Child> mList; // this is the issue
  StudentsList({this.mList});

对于这个:

class StudentsList extends StatefulWidget {
  @override
  _StudentsListState createState() => _StudentsListState();
}

class _StudentsListState extends State<StudentsList> {
  // final List<Child> mList; // Do not mark this as final

  List<Child> mList;

  ...

}

工作存储库

您可能会查看与您的问题密切相关的工作存储库。Github https://github.com/ejabu/flutter_sqlist_list

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

flutter :不支持的操作:无法添加到不可修改的列表 的相关文章

随机推荐