簡體   English   中英

Flutter:已關閉的可關閉小部件仍然是樹的一部分

[英]Flutter: a dismissed dismissible widget is still part of the tree

我正在構建一個健身應用程序(這里的圖片:健身應用程序示例),用戶可以在其中記錄他們的設置。 在我的應用程序中使用可關閉小部件時遇到問題。 滑動刪除功能會發送以下異常:已關閉的可關閉小部件仍然是樹的一部分

當滑動刪除單個集合時,我仍然需要保留用戶放入其他集合的信息。 我相信這是密鑰的問題,但是我已經嘗試過 UniqueKey()(它會重置所有其他輸入字段)和下面的示例。

如何使用dismissible刪除單個集合並仍然保留其他集合的其余用戶數據? 謝謝。

late List count = [0];

ListView.builder(
                shrinkWrap: true,
                itemCount: _count.length,
                itemBuilder: (context, index) {
                  // Create a new variable to display the set
                  int setNumber = index + 1;
                  return Dismissible(
                    key: ValueKey(_count[index]),
                    background: _swipeStyle(),
                    onDismissed: (direction) {
                      // Remove the item from the data source.
                      setState(() {
                        _count.removeAt(index);
                      });
                    },
                    child: Row(
                      children: [
                        Expanded(flex: 1, child: Text('Set $setNumber')),
                        Expanded(flex: 2, child: _buildWeight(index)),
                        const SizedBox(
                          width: 24.0,
                        ),
                        Expanded(flex: 2, child: _buildReps(index)),
                      ],
                    ),
                  );
                },
              ),

由於 Key 基於整數列表,所以可能有重復的鍵? 在這種情況下,框架將不知道刪除了哪個項目,並會觸發您剛剛發現的錯誤。

一個可能的解決方案是為每個項目分配一個唯一的 ID,這樣你就永遠不會有重復的鍵。

嘗試用UniqueKey()替換key: ValueKey(_count[index]) UniqueKey()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM