gpt4 book ai didi

flutter - 类型 'List' 不是类型转换中类型 'List' 的子类型

转载 作者:行者123 更新时间:2023-12-04 08:00:36 28 4
gpt4 key购买 nike

运行后 Dart 迁移 & 应用 null 安全,该错误在我的代码中弹出,我认为这是导致代码块的错误。

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@jamesdlin但推荐使用 whereType .例如:

List<Widget?> nullableWidgets = [];
List<Widget> nonNullable = nullableWidgets.whereType<Widget>().toList();
要回答您的问题:
Stack(
children: <Widget?>[
// Widgets (some of them nullable)
].whereType<Widget>().toList(),
)

关于flutter - 类型 'List<Widget?>' 不是类型转换中类型 'List<Widget>' 的子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66498385/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com