我正在关注这个例子。
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);
}
}
答案 0 :(得分:1)
您的 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;
}
}