首页 > 解决方案 > GlobalKey with an ArrayList

问题描述

I have a generated GlobalKey list, and I stucked with this:

final List<GlobalKey> keys = List<GlobalKey>.generate(optionalChars.length, (index) => GlobalKey(debugLabel: "key$index"));

Any way to use it in a list?

 Container(
        key: index != -1 ? keys[index] : null,

And I got the same error

The following assertion was thrown building Container(bg: BoxDecoration(color: Color(0xffdadada)), constraints: BoxConstraints(w=360.0, h=270.0)): Multiple widgets used the same GlobalKey.

Anyone can help?

标签: flutterdart

解决方案


This error happens if you use the same GlobalKey on different widgets. I replicated the code you gave and had no error except for when I attached the same GlobalKey object to another widget. So if you're using keys somewhere else this error will occur. You can't reuse the GlobalKey on two widgets at the same time.


推荐阅读