【问题标题】:Dismissible widget not changing when using Unique Key使用唯一键时可关闭的小部件不会更改
【发布时间】:2020-04-12 17:26:03
【问题描述】:

所以我尝试在 Flutter 代码中使用可关闭的小部件。它工作正常,除非我在第一个文档之前或在最后一个文档之后滑动。以下是我使用的代码示例:

StreamBuilder <QuerySnapshot>(
          stream: _firestore.collection('articles').snapshots(),
          builder: (BuildContext context, snapshot) {
            if (snapshot.hasError) {
              return Center(child: Text("Error fetching posts ${snapshot.error}"),);
            }
            if (snapshot.hasData) {
              List<DocumentSnapshot> documents = snapshot.data.documents;
              return GestureDetector(
                onHorizontalDragUpdate: (dragUpdateDetails) {
                  Navigator.push(context,
                      MaterialPageRoute(builder: (context) => WebViewContainer(documents[index].data['url'])));              },
                child: Dismissible(
                  key: Key(index.toString()),
                  direction: DismissDirection.vertical,
                  onDismissed: (direction) {
                    setState(() {
                      documents.removeAt(index);
                      index = (index==documents.length -1) ? index = 0 : index++;
                    });
                  },

代码运行良好,直到我到达文档列表的末尾。我收到的错误消息如下:

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following assertion was thrown building Dismissible-[<'0'>](dirty, state: _DismissibleState#6b64c(tickers: tracking 2 tickers)):
A dismissed Dismissible widget is still part of the tree.


Make sure to implement the onDismissed handler and to immediately remove the Dismissible widget from the application once that handler has fired.

The relevant error-causing widget was: 
  Dismissible-[<'0'>] file:///C:/Users/Lenovo/AndroidStudioProjects/sevenlines/lib/widgets/newspage.dart:63:24
When the exception was thrown, this was the stack: 
#0      _DismissibleState.build.<anonymous closure> (package:flutter/src/widgets/dismissible.dart:526:11)
#1      _DismissibleState.build (package:flutter/src/widgets/dismissible.dart:535:8)
#2      StatefulElement.build (package:flutter/src/widgets/framework.dart:4334:27)
#3      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4223:15)
#4      Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
...
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
A dismissed Dismissible widget is still part of the tree.
The relevant error-causing widget was: 
  Dismissible-[<'0'>] file:///C:/Users/Lenovo/AndroidStudioProjects/sevenlines/lib/widgets/newspage.dart:63:24
════════════════════════════════════════════════════════════════════════════════════════════════════

在检查此问题的解决方案后,我决定从依赖索引的键切换到 UniqueKey。但现在它根本不会在列表中移动——页面停留在第一个文档本身。请指教。

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore dismissible


    【解决方案1】:

    尝试在key: Key(index.toString()), 中使用 ObjectKey 而不是 Key 并将您的对象数据作为参数。

    【讨论】:

    • 抱歉,我得到了与 UniqueKey 相同的输出,即页面停留在第一个文档本身。我使用了 key:ObjectKey(documents[index]);
    • 你使用的是有状态的还是无状态的小部件?
    • 我正在使用有状态的小部件。
    • 让我们检查 Dismissible 的问题。你可以通过双击更改删除项目的代码吗?
    • 我该怎么做?删除项目是由可关闭的权利完成的,而不是手势检测器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    相关资源
    最近更新 更多