Flutter: 使用于多个小部件的ReorderableListViewChildGlobalKey错误

huangapple go评论62阅读模式
英文:

Flutter: ReorderableListViewChildGlobalKey used by Multiple widgets error

问题

以下是您要翻译的内容:

"Error I get is: The key [_ReorderableListViewChildGlobalKey ValueKey#46dc3] was used by multiple widgets

Each item in my list is already uniquely identified in my dismissable widget.

Widget mainCardWidget(BuildContext context) {
return ReorderableListView(
onReorder: onReorder,
physics: const BouncingScrollPhysics(
parent: AlwaysScrollableScrollPhysics()),
children: _getListItems(),
);
}

void onReorder(int oldIndex, int newIndex) {
setState(() {
if (newIndex > oldIndex) {
newIndex -= 1;
}
final Rung element = _items.removeAt(oldIndex);
_items.insert(newIndex, element);
});
}

List<Widget> _getListItems() => _items
.asMap()
.map((i, item) => MapEntry(i, _buildTenableListTile(item, i)))
.values
.toList();

Widget _buildTenableListTile(Rung item, int index) {
return Dismissible(
key: Key(item.rungId),
onDismissed: (direction) {
setState(() {
_items.removeAt(index);
_removeditems.add(item);
});
},
background: Container(color: Colors.red),
child: Padding(
padding: const EdgeInsets.all 20.0),
child: Card(child: cardWithInfoPage(item, context, index)),
),
);
}
}"

英文:

Error I get is: The key [_ReorderableListViewChildGlobalKey ValueKey#46dc3] was used by multiple widgets

Each item in my list is already uniquely identified in my dismissable widget.

Widget mainCardWidget(BuildContext context) {
    return ReorderableListView(
      onReorder: onReorder,
      physics: const BouncingScrollPhysics(
          parent: AlwaysScrollableScrollPhysics()),
      children: _getListItems(),
    );
  }

  void onReorder(int oldIndex, int newIndex) {
    setState(() {
      if (newIndex &gt; oldIndex) {
        newIndex -= 1;
      }
      final Rung element = _items.removeAt(oldIndex);
      _items.insert(newIndex, element);
    });
  }

  List&lt;Widget&gt; _getListItems() =&gt; _items
      .asMap()
      .map((i, item) =&gt; MapEntry(i, _buildTenableListTile(item, i)))
      .values
      .toList();

  Widget _buildTenableListTile(Rung item, int index) {
    return Dismissible(
      key: Key(item.rungId),
      onDismissed: (direction) {
        setState(() {
          _items.removeAt(index);
          _removeditems.add(item);
        });
      },
      background: Container(color: Colors.red),
      child: Padding(
        padding: const EdgeInsets.all(20.0),
        child: Card(child: cardWithInfoPage(item, context, index)),
      ),
    );
  }

答案1

得分: 1

我用 "key: UniqueKey()" 替换了我的 Dismissible widget 的键,从而解决了这个问题。

英文:

I replaced the key with key: UniqueKey() to my Dismissible widget and that solved this problem.

huangapple
  • 本文由 发表于 2023年4月10日 19:27:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976681.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定