Home > OS >  void Function(List<ProductModel>) isn't a valid override of void Function(List<Product
void Function(List<ProductModel>) isn't a valid override of void Function(List<Product

Time:01-01

there is a entity called Category and there is a model that extends the Category called CategoryModel now the Category entity has a variable called products which is a List of Product, and Product is an entity and has a model that extends it called ProductModel.

the problem in vscode

category_model.dart

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

what I want is to use A List of ProductModel in CategoryModel, instead of the Product entity.

PS: The models of each entity contain's methods like toJson, toEntity, fromJson, fromEntity

CodePudding user response:

The List<ProductModel> products; already coming from super class(Category) being extended.

You can do

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

You can just use like


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