【问题标题】:Flutter ListView doesn't refreshing itselfFlutter ListView 不会自行刷新
【发布时间】:2022-07-06 21:02:06
【问题描述】:

enter image description here

enter image description here

我有一个这样的应用程序。当我按下表示“添加学生”的绿色按钮时,我的应用会转到包含如下输入按钮的页面。

当我按“Kaydet”表示保存时,不会将学生添加到列表视图中。但是当我点击某个东西时,学生会出现。我怎么知道呢。

下面的代码。

Expanded(
      child: ListView.builder(
        itemCount: students.length,
        itemBuilder: (BuildContext context, int index) {
          return ListTile(
            leading: CircleAvatar(
              backgroundImage: NetworkImage(students[index].profilePhoto),
            ),
            title: Text(
                students[index].firstName + " " + students[index].lastName),
            subtitle: Text("Sınavdan aldığı not: " +
                students[index].grade.toString() +
                " [" +
                students[index].getStatus +
                "]"),
            trailing: buildStatusIcon(students[index].grade),
            onTap: () {
              setState(() {
                selectedStudent = students[index];
              });
            },
          );
        },
      ),
    ),

和其他文件

Widget buildSubmitButton() {
    return ElevatedButton(
      child: const Text("Kaydet"),
      onPressed: () {
        if (formKey.currentState!.validate()) {
          formKey.currentState!.save();
          students!.add(student);
          Navigator.pop(context);
        }
      },
    );
  }

【问题讨论】:

  • students!.add(student); 必须在 setState();

标签: flutter dart


【解决方案1】:

我认为您在“添加学生”按钮中使用了 Navigator.push,Navigator.push 有一个 .then 属性,请尝试使用 setState 听到

Navigator.push(context,route).then((value) {
                        setState(() {});
                      });

【讨论】:

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