flutter - 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?
解决方案
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.
推荐阅读
- r - 从 HTAFeatureSet 表达式数据中获取探针名称
- shell - 在 ssh 终端与 ubuntu 桌面上运行相同的命令,在 ssh 终端上无法运行?
- c# - 根据任何模型的选定属性生成输入
- c - 是否有任何可用的 mbed 以太网接口库?
- c++ - 乱码控制台输出管道 stderr 到更多
- regex - 在给定模式之前进行递归匹配
- apache - 是否有其他方式在本地主机上发送电子邮件,而无需在 xampp 上启动 apache
- android - 有没有办法将配置文件文件从客户端应用程序传递到库项目?
- php - 从相关表中获取属性作为外键
- java - 如何在Maven项目中导入OpenCV?