简体   繁体   English

被关闭的 Dismissible 小部件仍然是树的一部分

[英]A dismissed Dismissible widget is still part of the tree

There seem to be many questions regarding this error but I'm yet to find an answer that will work in my situation.关于这个错误似乎有很多问题,但我还没有找到适合我的情况的答案。

The behaviour I'm seeing is that the Dismissible works, it fires and deletes the item, but for a moment it shows an error in the ListView.我看到的行为是 Dismissible 工作,它触发并删除项目,但有一段时间它在 ListView 中显示错误。 I'm guessing it's waiting for the tree to update based on the Stream<List>, which in turn is removing the record from Firebase.我猜它正在等待树根据 Stream<List> 更新,这反过来又从 Firebase 中删除记录。

My StreamBuilder...我的流构建器...

return StreamBuilder<List<Person>>(
      stream: personBloc.personsByUserId(userId),
      builder: (context, snapshot) {

...

}

My ListView.builder()我的 ListView.builder()

ListView.builder(
                      itemCount: snapshot.data.length,
                      itemBuilder: (context, index) {
                        var person = snapshot.data[index];

                        return GestureDetector(
                          onTap: () {
                            Navigator.of(context)
                                .pushNamed('/reading/${person.personId}');
                          },
                          child: Dismissible(
                            key: Key(person.personId),
                            direction: DismissDirection.endToStart,
                            onDismissed: (direction) {
                              personBloc.deletePerson(person.personId);
                            },
                            background: Container(
                              child: Padding(
                                padding: const EdgeInsets.all(15.0),
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.end,
                                  children: [
                                    Icon(
                                      FontAwesomeIcons.trash,
                                      color: Colors.white,
                                    ),
                                    Text(
                                      'Delete',
                                      style: TextStyle(color: Colors.white),
                                      textAlign: TextAlign.right,
                                    ),
                                  ],
                                ),
                              ),
                              color: Colors.red,
                            ),
                            child: AppCard(
                              //Bunch of properties get set here
                            ),
                          ),
                        );
                      },
                    

My deletePerson我的删除人

  deletePerson(String personId) async {
    fetchPersonId(personId).then((value) {
      if (value.imageUrl.isNotEmpty) {
        removeImage();
      }

      db.deletePerson(personId);
    });
  }

I've tried changing the onDismissed to a confirmDismiss with no luck.我试过将 onDismissed 更改为 confirmDismiss ,但没有运气。

在此处输入图片说明

Any suggestions?有什么建议?

This happens when you dismiss with a Dismissible widget but haven't removed the item from the list being used by the ListView.builder .当您使用Dismissible小部件关闭但尚未从ListView.builder使用的列表中删除项目时,就会发生这种情况。 If your list was being stored locally, with latency not being an issue, you might never see this issue, but because you are using Firestore (I assume, based on your mention of Firebase ) then there is going to be some latency between asking the item to be removed from the DB and the list getting updated on the app.如果您的列表存储在本地,延迟不是问题,您可能永远不会看到这个问题,但是因为您使用的是Firestore (我假设,根据您提到的Firebase ),那么询问之间会有一些延迟要从DB删除的项目和应用程序上更新的列表。 To avoid this issue, you can manage the local list separately from the list coming from the Stream .为避免此问题,您可以将本地列表与来自Stream列表分开管理。 Updating the state as the stream changes, but allowing you to delete items locally from the local list and avoiding these kind of view errors.在流更改时更新状态,但允许您从本地列表中本地删除项目并避免此类视图错误。

I ended up making a couple of changes to my code to address this.我最终对我的代码进行了一些更改来解决这个问题。

I added a BehaviourSubject in my bloc to monitor whether the delete was taking place or not.我在我的 bloc 中添加了一个 BehaviourSubject 来监控删除是否正在发生。 At the beginning of the firestore delete I set this to true and then added a .then to the delete to set it back to false.在 firestore delete 开始时,我将其设置为 true,然后将 .then 添加到 delete 以将其设置回 false。

I then added a Streambuilder around the ListView on the screen to monitor the value of this and show a CircularProgressIndicator when true.然后我在屏幕上的 ListView 周围添加了一个 Streambuilder 来监视它的值并在为 true 时显示一个 CircularProgressIndicator。

It now looks like this:现在看起来像这样:

在此处输入图片说明

Thanks for your help.谢谢你的帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter:已关闭的可关闭小部件仍然是树的一部分 - Flutter: a dismissed dismissible widget is still part of the tree 已关闭的 Dismissible 小部件仍然是 flutter 中树的一部分 - A dismissed Dismissible widget is still part of the tree in flutter 如何修复“已关闭的 Dismissible 小部件仍然是树的一部分。” 在 flutter - How to fix 'A dismissed Dismissible widget is still part of the tree.' in flutter 已关闭的 Dismissible 小部件仍然是树的一部分。 在 flutter - A dismissed Dismissible widget is still part of the tree. In flutter Flutter Dismissed Dismissible 小部件仍然是树问题的一部分,无法解决 - Flutter Dismissed Dismissible widget still part of tree issue, can't resolve 如何解决问题:使用 Bloc 时“已关闭的 Dismissible 小部件仍然是树的一部分” - How to resolve issue: “A dismissed Dismissible widget is still part of the tree” while use Bloc 已关闭的 Dismissible 小部件仍然是树的一部分。 从列表中删除项目后重复出现此错误 - A dismissed Dismissible widget is still part of the tree. Getting this error repeatedly after removing item from list `一个被解雇的 Dismissible 小部件仍然是树的一部分`错误突然发生(FutureBuilder,ListView) - `A dismissed Dismissible widget is still part of the tree` error occurred suddenly (FutureBuilder, ListView) 如何修复“已关闭的 Dismissible 小部件仍然是树的一部分。” flutter 错误 - How to fix "A dismissed Dismissible widget is still part of the tree." error in flutter 在 Flutter 中,是否可以增加 Dismissible 小部件的透明度? - In Flutter is it possible to increase the transparency of a Dismissible widget the further it is dismissed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM