【问题标题】:flutter : Unsupported operation: Cannot add to an unmodifiable list颤振:不支持的操作:无法添加到不可修改的列表
【发布时间】:2019-12-24 05:25:39
【问题描述】:

我在 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],
            ),
          );
        },
      ),
    );
  }
}

【问题讨论】:

    标签: list flutter dart


    【解决方案1】:

    Stateless Widget 属性是不可变的

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

    为什么?

    因为 Flutter 期望 没有业务逻辑驻留 在 StatelessWidget 中。 如果我们需要在 Student 列表中添加新的 Student,这被认为是业务逻辑。 如果我们需要删除Student列表中的某个Student,则视为业务逻辑。

    所以通过使用无状态小部件,Flutter 将只关注它在屏幕上的显示方式、宽度是多少、约束等。

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

    类似于我们的大学生活。我们在最终报告中标记的成绩,即使在我们大学毕业后也不会改变。正如在Final Report中所说,它必须是final。

    Stateful Widget 属性是可变的

    为什么? 因为 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

    【讨论】:

      【解决方案2】:

      Stateless Widgets 属性不能是不可变的,简单来说就是它不应该包含任何非最终变量。

      只需将其转换为有状态小部件并在 class _StudentsListState 内创建不带 final 关键字的变量,因为您正在修改该列表的值。

      【讨论】:

        猜你喜欢
        • 2021-12-26
        • 1970-01-01
        • 2019-08-19
        • 2022-10-01
        • 2020-03-14
        • 2014-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多