参数类型“Widget Function(Categoria)”不能分配给参数类型“dynamic Function(Child)”。 (型号)颤振
Posted
技术标签:
【中文标题】参数类型“Widget Function(Categoria)”不能分配给参数类型“dynamic Function(Child)”。 (型号)颤振【英文标题】:The argument type 'Widget Function(Categoria)' can't be assigned to the parameter type 'dynamic Function(Child)'. (Model) Flutter 【发布时间】:2021-08-22 12:49:05 【问题描述】:我正在学习这个例子。
https://esflutter.dev/docs/catalog/samples/expansion-tile-sample
使类别中的多级可扩展。我有一个最多包含 3 个类别的树。
但它给了我这个错误:我把它放在图像中,以便可以看到哪个部分标记了错误:
由于我比较新,我一直在尝试解决它好几天,但我没有意识到它可能是,我也将我的模型留在下面,因为我认为存在错误,也许还有其他方法。地图?或者我没有意识到解决方案,请如果有人可以帮助我。
// 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;
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);
【问题讨论】:
你能添加你的 _buildTiles 方法吗?您似乎与该方法中的类型不匹配。还有root.children
的类型是什么?
@croxx5f 我认为该方法是您在图像中看到的方法,无论如何我放了完整的类,.children
是 List您的Categoria
和Child
模型似乎共享了它们几乎所有的属性。问题是_buildTiles
需要Categoria
而Categoria
的孩子是Child
。
如果合并 Child
和 Categoria
模型,您可以执行以下操作。
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((e)=>_buildTiles(e)).toList(),
);
@override
Widget build(BuildContext context)
return _buildTiles(entry);
void main()
runApp(ExpansionTileSample());
class Categoria
Categoria(
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 Categoria.fromJson(Map<String, dynamic> json) => Categoria(
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,
;
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;
【讨论】:
这行得通,谢谢,但正如您在代码中看到的,在扩展瓦片子项中有一个逻辑,它告诉颤振“这是以下类别树”children: root.children.map(_buildTiles).toList()
,如果我需要给它从第二个类别树的边距,也就是说,每个都可以在另一个中扩展,我该怎么做?我尝试了各种方法,但没有找到细节。以上是关于参数类型“Widget Function(Categoria)”不能分配给参数类型“dynamic Function(Child)”。 (型号)颤振的主要内容,如果未能解决你的问题,请参考以下文章
获取错误“未知”类型的参数不能分配给“错误”类型的参数 |空值'
参数类型“Type”不能分配给参数类型“FirebaseUser”