运行后飞镖迁移&应用空安全性,该错误在我的代码中弹出,我认为这是导致代码块的错误。
LayoutBuilder(builder: (context, cons) {
return GestureDetector(
child: new Stack(
children: <Widget?>[
// list of different widgets
.
.
.
].where((child) => child != null).toList(growable: true) as List<Widget>,
),
);
),
错误消息显示:
The following _CastError was thrown building LayoutBuilder:
type 'List<Widget?>' is not a subtype of type 'List<Widget>' in type cast
The relevant error-causing widget was
LayoutBuilder
package:projectName/…/components/fileName.dart:182
如果有人遇到这个问题,如何解决?
尽管提供了不同的解决方法here by @詹姆斯德林但推荐的方法是使用whereType。例如:
List<Widget?> nullableWidgets = [];
List<Widget> nonNullable = nullableWidgets.whereType<Widget>().toList();
回答你的问题:
Stack(
children: <Widget?>[
// Widgets (some of them nullable)
].whereType<Widget>().toList(),
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)