flutter void函数(列表< ProductModel>)不是void函数(列表)的有效覆盖< Product>

egmofgnx  于 2023-01-02  发布在  Flutter
关注(0)|答案(1)|浏览(130)

存在称为类别的实体,并且存在扩展该类别的称为类别模型的模型,现在类别实体具有称为产品的变量,该变量是产品的列表,并且产品是实体并且具有扩展该实体的称为产品模型的模型。
the problem in vscode
第一个月

import 'package:flutter/foundation.dart' hide Category;
import 'package:saleor_app_flutter/features/storefront/data/models/product_model.dart';

import '../../domain/entities/category.dart';

class CatergoryModel extends Category {
  String id;
  String name;
  String? desciption;
  String? backgroundImage;
  List<ProductModel> products; // Here is the error !!
  CatergoryModel({
    required this.id,
    required this.name,
    this.desciption,
    this.backgroundImage,
    required this.products,
  }) : super(
            id: id,
            name: name,
            products: products,
            backgroundImage: backgroundImage,
            desciption: desciption);

  CatergoryModel copyWith({
    String? id,
    String? name,
    String? desciption,
    String? backgroundImage,
    List<ProductModel>? products,
  }) {
    return CatergoryModel(
      id: id ?? this.id,
      name: name ?? this.name,
      desciption: desciption ?? this.desciption,
      backgroundImage: backgroundImage ?? this.backgroundImage,
      products: products ?? this.products,
    );
  }

 

  @override
  bool operator ==(covariant CatergoryModel other) {
    if (identical(this, other)) return true;

    return other.id == id &&
        other.name == name &&
        other.desciption == desciption &&
        other.backgroundImage == backgroundImage &&
        listEquals(other.products, products);
  }

  @override
  int get hashCode {
    return id.hashCode ^
        name.hashCode ^
        desciption.hashCode ^
        backgroundImage.hashCode ^
        products.hashCode;
  }
}

}

category.dart

class Category {
  String id;
  String name;
  String? desciption;
  String? backgroundImage;
  List<Product> products;
  Category({
    required this.id,
    required this.name,
    this.desciption,
    this.backgroundImage,
    required this.products,
  });

  @override
  bool operator ==(covariant Category other) {
    if (identical(this, other)) return true;

    return other.id == id &&
        other.name == name &&
        other.desciption == desciption &&
        other.backgroundImage == backgroundImage &&
        listEquals(other.products, products);
  }

  @override
  int get hashCode {
    return id.hashCode ^
        name.hashCode ^
        desciption.hashCode ^
        backgroundImage.hashCode ^
        products.hashCode;
  }
}

product.dart

class Product {
  String id;
  String name;
  String description;
  String thumbnail;
  String currency;
  String amount;
  List<String> media;
  Product({
    required this.id,
    required this.name,
    required this.description,
    required this.thumbnail,
    required this.currency,
    required this.amount,
    required this.media,
  });

  @override
  bool operator ==(covariant Product other) {
    if (identical(this, other)) return true;
  
    return 
      other.id == id &&
      other.name == name &&
      other.description == description &&
      other.thumbnail == thumbnail &&
      other.currency == currency &&
      other.amount == amount &&
      listEquals(other.media, media);
  }

  @override
  int get hashCode {
    return id.hashCode ^
      name.hashCode ^
      description.hashCode ^
      thumbnail.hashCode ^
      currency.hashCode ^
      amount.hashCode ^
      media.hashCode;
  }
}

product_model.dart

class ProductModel extends Product {
  String id;
  String name;
  String description;
  String thumbnail;
  String currency;
  String amount;
  List<String> media;
  ProductModel(
      {required this.id,
      required this.name,
      required this.description,
      required this.thumbnail,
      required this.currency,
      required this.amount,
      required this.media})
      : super(
          id: id,
          name: name,
          description: description,
          thumbnail: thumbnail,
          currency: currency,
          amount: amount,
          media: media,
        );

  @override
  bool operator ==(covariant ProductModel other) {
    if (identical(this, other)) return true;

    return other.id == id &&
        other.name == name &&
        other.description == description &&
        other.thumbnail == thumbnail &&
        other.currency == currency &&
        other.amount == amount &&
        listEquals(other.media, media);
  }

  @override
  int get hashCode {
    return id.hashCode ^
        name.hashCode ^
        description.hashCode ^
        thumbnail.hashCode ^
        currency.hashCode ^
        amount.hashCode ^
        media.hashCode;
  }
}

我想要的是使用CategoryModel中的ProductModel列表,而不是Product实体。
PS:每个实体的模型都包含toJsontoEntityfromJsonfromEntity等方法

xdyibdwo

xdyibdwo1#

List<ProductModel> products;已经来自正在扩展的超类(Category)。
你能做到

class CatergoryModel extends Category {
  String id;
  String name;
  String? desciption;
  String? backgroundImage;
 
  CatergoryModel({
    required this.id,
    required this.name,
    this.desciption,
    this.backgroundImage,
    required List<Product> products,
  }) : super(
            id: id,
            name: name,
            products: products,
            backgroundImage: backgroundImage,
            desciption: desciption);

你可以用

class CatergoryModel extends Category {
  CatergoryModel({
    required super.id,
    required super.name,
    required super.desciption,
    required super.backgroundImage,
    required super.products,
  });

相关问题