Flutter:根据属性从 class 调用方法
Flutter: Call a method from a class based on properties
objective是利用父class的属性调用一个方法,扩展class的内容。
以下代码旨在调用基于其父 class 的 属性 的方法。它 returns 类型错误。
它是这样称呼的:
MyToolbar(data: [
{
'MySecondClass': ['red','green','purple']
}
])
这是 class:
class MyToolbar extends StatelessWidget {
MyToolbar({required this.data})
final List data;
ToolbarContent(type, data) {
if (type == 'MySecondClass') {
return MySecondClass(toggles: data);
}
@override
Widget build(BuildContext context) {
return Stack(children:[
for (List childData in data)
ToolbarContent('mySecondClass', childData),
])}
首先这个returns下面的类型错误。
_TypeError (type '_InternalLinkedHashMap<String, List<String>>' is not a subtype of type 'List<dynamic>')
其次,列表需要找到属性数据的键,以便为函数'ToolbarContent'设置正确的函数名称。
默认情况下,Dart 将任何列表设置为 List<dynamic>
。这就是错误所说的。你需要投射你的名单,试试这个 final List<Map<String, List<String>> data;
这里有一些问题。首先,如temp_所述,您需要为data
设置列表类型,在本例中为List<Map<String,List<String>>
其次是 for (List childData in data)
实际上需要 for (Map<String,List<String>> childData in data)
第三个是假设,但我认为您的 for
循环中存在错字,其中 mySecondClass
应该是 MySecondClass
(或其他方式)
正确的代码如下:
class MyToolbar extends StatelessWidget {
final List<Map<String, List<String>>> data;
MyToolbar({required this.data});
@override
Widget build(BuildContext context) {
var children = <Widget>[];
data.forEach((childData) {
childData.forEach((key, stringList) {
//I'm assuming Toolbar content takes the key of the map i.e. MySecondClass
//as the first param and the List for the key as the second param
children.add(ToolbarContent(key, stringList));
});
});
return Stack(
children: children,
);
}
}
注意:我还假设 ToolbarContent
是另一个 class,但如果不是,请告诉我。
objective是利用父class的属性调用一个方法,扩展class的内容。
以下代码旨在调用基于其父 class 的 属性 的方法。它 returns 类型错误。
它是这样称呼的:
MyToolbar(data: [
{
'MySecondClass': ['red','green','purple']
}
])
这是 class:
class MyToolbar extends StatelessWidget {
MyToolbar({required this.data})
final List data;
ToolbarContent(type, data) {
if (type == 'MySecondClass') {
return MySecondClass(toggles: data);
}
@override
Widget build(BuildContext context) {
return Stack(children:[
for (List childData in data)
ToolbarContent('mySecondClass', childData),
])}
首先这个returns下面的类型错误。
_TypeError (type '_InternalLinkedHashMap<String, List<String>>' is not a subtype of type 'List<dynamic>')
其次,列表需要找到属性数据的键,以便为函数'ToolbarContent'设置正确的函数名称。
默认情况下,Dart 将任何列表设置为 List<dynamic>
。这就是错误所说的。你需要投射你的名单,试试这个 final List<Map<String, List<String>> data;
这里有一些问题。首先,如temp_所述,您需要为data
设置列表类型,在本例中为List<Map<String,List<String>>
其次是 for (List childData in data)
实际上需要 for (Map<String,List<String>> childData in data)
第三个是假设,但我认为您的 for
循环中存在错字,其中 mySecondClass
应该是 MySecondClass
(或其他方式)
正确的代码如下:
class MyToolbar extends StatelessWidget {
final List<Map<String, List<String>>> data;
MyToolbar({required this.data});
@override
Widget build(BuildContext context) {
var children = <Widget>[];
data.forEach((childData) {
childData.forEach((key, stringList) {
//I'm assuming Toolbar content takes the key of the map i.e. MySecondClass
//as the first param and the List for the key as the second param
children.add(ToolbarContent(key, stringList));
});
});
return Stack(
children: children,
);
}
}
注意:我还假设 ToolbarContent
是另一个 class,但如果不是,请告诉我。