Android 参数类型';小部件功能(分类)和#x27;can';不能分配给参数类型';动态函数(子函数)';。(模型)颤振
我遵循这个例子 使类别中的多个级别可扩展。我有一个多达3个类别的树 但它给了我这个错误:我把它放在图像中,这样就可以看到哪个部分标记了错误: 由于我相对较新,我已经尝试解决它好几天了,但我没有意识到它可能是,我也将我的模型留在下面,因为我认为存在错误,也许有其他方法,而不是.map?或者我不知道解决办法,如果有人能帮我Android 参数类型';小部件功能(分类)和#x27;can';不能分配给参数类型';动态函数(子函数)';。(模型)颤振,android,flutter,model,multi-level,expandable,Android,Flutter,Model,Multi Level,Expandable,我遵循这个例子 使类别中的多个级别可扩展。我有一个多达3个类别的树 但它给了我这个错误:我把它放在图像中,这样就可以看到哪个部分标记了错误: 由于我相对较新,我已经尝试解决它好几天了,但我没有意识到它可能是,我也将我的模型留在下面,因为我认为存在错误,也许有其他方法,而不是.map?或者我不知道解决办法,如果有人能帮我 // To parse this JSON data, do // // final categorias = categoriasFromJson(jsonStri
// To parse this JSON data, do
//
// final categorias = categoriasFromJson(jsonString);
import 'dart:convert';
Categorias categoriasFromJson(String str) => Categorias.fromJson(json.decode(str));
String categoriasToJson(Categorias data) => json.encode(data.toJson());
class Categorias {
Categorias({
this.res,
this.id,
this.empresa,
this.idioma,
this.categorias,
});
int res;
int id;
int empresa;
String idioma;
List<Categoria> categorias;
factory Categorias.fromJson(Map<String, dynamic> json) => Categorias(
res: json["res"],
id: json["id"],
empresa: json["empresa"],
idioma: json["idioma"],
categorias: List<Categoria>.from(json["categorias"].map((x) => Categoria.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"res": res,
"id": id,
"empresa": empresa,
"idioma": idioma,
"categorias": List<dynamic>.from(categorias.map((x) => x.toJson())),
};
}
class Child {
Child({
this.id,
this.name,
this.img,
this.titulo,
this.children,
this.baos,
});
int id;
String name;
String img;
int titulo;
List<Categoria> children;
String baos;
factory Child.fromJson(Map<String, dynamic> json) => Child(
id: json["id"],
name: json["name"] == null ? null : json["name"],
img: json["img"],
titulo: json["titulo"],
children: json["children"] == null ? null : List<Categoria>.from(json["children"].map((x) => Categoria.fromJson(x))),
baos: json["Baños"] == null ? null : json["Baños"],
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name == null ? null : name,
"img": imgValues.reverse[img],
"titulo": titulo,
"children": children == null ? null : List<dynamic>.from(children.map((x) => x.toJson())),
"Baños": baos == null ? null : baos,
};
}
class Categoria {
Categoria({
this.id,
this.name,
this.img,
this.titulo,
this.children,
});
int id;
String name;
String img;
int titulo;
List<Child> children;
factory Categoria.fromJson(Map<String, dynamic> json) => Categoria(
id: json["id"],
name: json["name"],
img: json["img"],
titulo: json["titulo"],
children: json["children"] == null ? null : List<Child>.from(json["children"].map((x) => Child.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
"img": imgValues.reverse[img],
"titulo": titulo,
"children": children == null ? null : List<dynamic>.from(children.map((x) => x.toJson())),
};
}
enum Img { FOTOSCIRCULOS_INSECTICIDA_PNG, EMPTY }
final imgValues = EnumValues({
"": Img.EMPTY,
"fotoscirculos/insecticida.png": Img.FOTOSCIRCULOS_INSECTICIDA_PNG
});
class EnumValues<T> {
Map<String, T> map;
Map<T, String> reverseMap;
EnumValues(this.map);
Map<T, String> get reverse {
if (reverseMap == null) {
reverseMap = map.map((k, v) => new MapEntry(v, k));
}
return reverseMap;
}
}
//要解析此JSON数据,请执行以下操作
//
//final categorias=categoriasFromJson(jsonString);
导入“dart:convert”;
Categorias categoriasFromJson(String str)=>Categorias.fromJson(json.decode(str));
字符串categoriasToJson(categoriasdata)=>json.encode(data.toJson());
类别{
分类({
这是我的,
这个身份证,
这是empresa,
这个成语,
这个分类,
});
国际关系;
int-id;
int empresa;
字符串惯用法;
列出分类;
工厂分类.fromJson(映射json)=>Categorias(
res:json[“res”],
id:json[“id”],
empresa:json[“empresa”],
idioma:json[“idioma”],
categorias:List.from(json[“categorias”].map((x)=>Categoria.fromJson(x)),
);
映射到JSON()=>{
“res”:res,
“id”:id,
“empresa”:empresa,
“成语”:成语,
“categorias”:List.from(categorias.map((x)=>x.toJson()),
};
}
班童{
孩子({
这个身份证,
这个名字,
这个.img,,
这是提特罗,
这个,孩子,
这是,,
});
int-id;
字符串名;
字符串img;
int titulo;
列出儿童名单;
串宝;
factory Child.fromJson(映射json)=>Child(
id:json[“id”],
name:json[“name”]==null?null:json[“name”],
img:json[“img”],
titulo:json[“titulo”],
children:json[“children”]==null?null:List.from(json[“children”].map((x)=>Categoria.fromJson(x)),
baos:json[“Baños”]==null?null:json[“Baños”],
);
映射到JSON()=>{
“id”:id,
“name”:name==null?null:name,
“img”:img值。反向[img],
“提特罗”:提特罗,
“children”:children==null?null:List.from(children.map((x)=>x.toJson()),
“Baños”:baos==null?null:baos,
};
}
A类{
范畴({
这个身份证,
这个名字,
这个.img,,
这是提特罗,
这个,孩子,
});
int-id;
字符串名;
字符串img;
int titulo;
列出儿童名单;
工厂Categoria.fromJson(映射json)=>Categoria(
id:json[“id”],
名称:json[“名称”],
img:json[“img”],
titulo:json[“titulo”],
children:json[“children”]==null?null:List.from(json[“children”].map((x)=>Child.fromJson(x)),
);
映射到JSON()=>{
“id”:id,
“姓名”:姓名,
“img”:img值。反向[img],
“提特罗”:提特罗,
“children”:children==null?null:List.from(children.map((x)=>x.toJson()),
};
}
枚举Img{FOTOSCIRCULOS_杀虫药_PNG,空}
最终imgValues=枚举值({
“”:Img.EMPTY,
“fotoscirculos/insecticida.png”:Img.fotoscirculos\u insecticida\u png
});
类枚举值{
地图;
地图反转地图;
枚举值(this.map);
地图反转{
if(reverseMap==null){
reverseMap=map.map((k,v)=>newMapEntry(v,k));
}
返回反向映射;
}
}
EntryItem类:
class BuyView extends StatefulWidget {
final List<Categoria> categorias;
const BuyView({Key key, this.categorias}) : super(key: key);
@override
_BuyViewState createState() => _BuyViewState();
}
class _BuyViewState extends State<BuyView> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('ExpansionTile'),
),
body: ListView.builder(
itemBuilder: (BuildContext context, int index) =>
EntryItem(widget.categorias[index]),
itemCount: widget.categorias.length,
),
),
);
}
}
// Displays one Entry. If the entry has children then it's displayed
// with an ExpansionTile.
class EntryItem extends StatelessWidget {
const EntryItem(this.entry);
final Categoria entry;
Widget _buildTiles(Categoria root) {
if (root.children.isEmpty) return ListTile(title: Text(root.name));
return ExpansionTile(
key: PageStorageKey<Categoria>(root),
title: Text(root.name),
children: root.children.map(_buildTiles).toList(),
);
}
@override
Widget build(BuildContext context) {
return _buildTiles(entry);
}
}
class BuyView扩展了StatefulWidget{
最终分类清单;
constBuyView({Key-Key,this.categorias}):super(Key:Key);
@凌驾
_BuyViewState createState()=>\u BuyViewState();
}
类_BuyViewState扩展状态{
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
appBar:appBar(
标题:常量文本(“扩展文件”),
),
正文:ListView.builder(
itemBuilder:(BuildContext上下文,int索引)=>
EntryItem(widget.categorias[index]),
itemCount:widget.categorias.length,
),
),
);
}
}
//显示一个条目。如果条目有子条目,则显示该条目
//使用扩展文件。
类EntryItem扩展了无状态小部件{
const EntryItem(此项);
最终分类条目;
Widget\u buildTiles(分类根){
if(root.children.isEmpty)返回ListTile(标题:Text(root.name));
返回扩展文件(
密钥:PageStorageKey(根),
标题:文本(根目录名称),
children:root.children.map(_buildTiles.toList(),
);
}
@凌驾
小部件构建(构建上下文){
返回(条目);;
}
}
您的Categoria
和Child
模型似乎共享几乎所有的属性。问题是\u buildTiles
需要一个Categoria
,而Categoria
的子对象是子对象
如果您将子类
和分类
模型合并,则可以执行以下操作
class EntryItem扩展了无状态小部件{
const EntryItem(此项);
最终分类条目;
Widget\u buildTiles(分类根){
if(root.children.isEmpty)返回ListTile(标题:Text(root.name));
返回扩展文件(
密钥:PageStorageKey(根),
标题:文本(根目录名称),
children:root.children.map((e)=>\u buildTiles(e)).toList(),
);
}
@凌驾
小部件构建(构建上下文){
返回(条目);;
}
}
void main(){
runApp(ExpansionTileSample());
}
A类{
范畴({
这个身份证,
这个名字,
这个.img,,
这是提特罗,
这个,孩子,
这是,,
});
int-id;
字符串名;
字符串img;