我有一个继承的小部件,我在这个继承的小部件中发送一个Map变量,然后我从子小部件中提取这个继承的小部件,并将这个map变量放在build方法中的map循环中,但我得到一个错误,如“The element type 'Map〈dynamic,dynamic〉' can't be assigned to the list type 'Widget'”。
这里是我的代码简化版本请不要为语法错误而烦恼我删除了一堆不必要的代码
class BasketData extends InheritedWidget {
BasketData({
Key? key,
required this.coffees,
required this.basket,
required Widget child,
}) : super(key: key, child: child);
final List<Map<String, Object>> coffees;
Map<String, int>? basket;
static BasketData of(BuildContext context) {
final BasketData? result = context.dependOnInheritedWidgetOfExactType<BasketData>();
assert(result != null, 'No BasketData found in context');
return result!;
}
@override
bool updateShouldNotify(BasketData old) {
return false;
}
}
class _MyHomePageState extends State<MyHomePage> {
List pages = [
CoffeePage(),
BasketPage()
];
Map<String, int> basket = {
"Americano": 1,
};
@override
Widget build(BuildContext context) {
return Scaffold(
body: BasketData(
coffees: coffees,
basket: basket,
child: pages[currentPage],
),
});
},
),
);
}
}
class _BasketPageState extends State<BasketPage> {
@override
Widget build(BuildContext context) {
final BasketDataa = BasketData.of(context);
return Center(
child: Column(
children: <Widget>[
BasketDataa.basket.map((key, value) { // ERROR IS HERE
Text("test")
}),
);
}
}
2条答案
按热度按时间zi8p0yeb1#
正如RichardHeap的评论所建议的那样,你不应该使用map函数来MapMap类。相反,你应该使用entries属性来生成一个widget列表。
所以,你可以这样做:
8i9zcol22#