无法将参数类型 '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

我按照这个例子。

https://esflutter.dev/docs/catalog/samples/expansion-tile-sample

在类别中进行多层次扩展。我有一个最多 3 个类别的树。

但它给了我这个错误:我把它放在图像中以便可以看到哪个部分标记了错误:

因为我比较新,几天来我一直在尝试解决这个问题,但我没有意识到它可能是,我也把我的模型留在下面,因为我认为有错误,也许有一些方法除了 .map?或者我没有意识到解决方案,如果有人可以帮助我。

// 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;
    }
}

条目项class:

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);
  }
}

您的 CategoriaChild 模型似乎共享它们几乎所有的属性。问题是 _buildTiles 期望 CategoriaCategoria 的 children 是 Child.

如果合并 ChildCategoria 模型,您可以执行以下操作。

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;
  }
}