如何从动态映射构建无状态小部件



我是flutter的新手,我想构建动态Map<键,值>到无状态小部件。我试过使用尽可能多的教程,但只有在列表中才能使用。如果是包含的地图

final Map to Check={'卧室':'床','浴室':'浴室_出线'};

它显示错误

lib/template/general。dart:164:18:错误:类型为"Row"的值不能从返回类型为"MapEntry<动态动态>'。

  • "Row"来自"package:flutter/src/widgets/basic.dart"("../flutter/packages/flutter/lib/src/widgets/basic.dart"(
  • "MapEntry"来自"dart:core"。return行(^lib/template/general-dart:170:12:错误:没有为类"Map<动态,动态>'
  • "Map"来自"dart:core"。请尝试将名称更正为现有方法的名称,或定义名为"toList"的方法。}).toList((,^^^^^^

这是无状态窗口小部件函数

class BundleSpecification extends StatelessWidget {
final Map specification;
BundleSpecification(this.specification);
final Map toCheck = {'bedroom':'bed','bathroom':'bathtub_outlined'};
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.fromLTRB(0, 15.0, 0, 15.0),
child: Row(
children: toCheck.map((type,value){
return Row(
children: [
Icon(Icons.bed),
Text(specification[value]),
],
);
}).toList(),
)
);
}

我真的很感激你的回答。非常感谢。

Row采用Iterable(例如List(作为children的参数。你给它提供了一张显然不起作用的地图。

您可以尝试通过以下方式在Row小部件中映射Map

Row(
children: [
...toCheck.entries.map(
(x) => Row(
children: [
Icon(Icons.bed),
Text(specification[x.value]),
],
),
)
],
)

相关内容

最新更新