【问题标题】:copyWith() method for Inheritance class on dartdart 上继承类的 copyWith() 方法
【发布时间】:2023-01-18 22:59:36
【问题描述】:

我想为我的班级实施 copyWith() 方法。由于它继承了类,我得到了错误:

被覆盖的成员。

我的基础模型

class BaseData {
  int id;
  String? createdAt, updatedAt;

  BaseData({this.id = 0, this.createdAt, this.updatedAt});
  BaseData copyWith(
      {int? id, String? createdAt, String? updatedAt}) {
    return BaseData(
        CreatedAt: createdAt ?? this.createdAt,
        ID: id ?? this.id,
        UpdatedAt: updatedAt ?? this.updatedAt);
  }
}

我还有另一个扩展到我的BaseData模型的课程

class QcData extends BaseData{
  String name;
  QcData ({this.name =""});

  @override
  QcData copyWith({
  String? name
  }){
    return QcData(name: name ?? this.name);
 }
}

如何为我的模型类创建copyWith()方法?

提前致谢。

【问题讨论】:

    标签: flutter dart oop


    【解决方案1】:

    继承意味着当您扩展或实现基类时,类成员的定义应保持不变,在您的情况下,通过执行以下操作:

      @override
       QcData copyWith({
       String? name
      }){
         return QcData(name: name ?? this.name);
     }
    

    此方法声明与 BaseData 不同,因为它们采用不同的参数集,您应该保持参数与 BaseData 相同,因此您的代码应该是这样才能工作:

    class QcData extends BaseData {
      String name;
      QcData({this.name = ""});
    
      @override
      QcData copyWith({
        int? id,
        String? createdAt,
        String? updatedAt,
      }) {
        return QcData(name: name ?? this.name);
      }
    }
    

    【讨论】:

      【解决方案2】:

      我们还需要在创建 QcData(child) 时传递数据。我更喜欢以这种方式修改构造函数。

        const QcData({
          this.name = "",
          super.id,
          super.createdAt,
          super.updatedAt,
        });
      

      现在我们可以在 copyWith 方法上访问这些属性,另一个原因是我喜欢将 const 构造函数与最终字段一起使用。

       @override
        QcData copyWith({
          String? name,
          int? id,
          String? createdAt,
          String? updatedAt,
        }) {
          return QcData(
            name: name ?? this.name,
            id: id ?? this.id,
            createdAt: createdAt ?? this.createdAt,
            updatedAt: updatedAt ?? this.updatedAt,
          );
        }
      

      这些模型将是

      class BaseData {
        final int id;
        final String? createdAt;
        final String? updatedAt;
      
        const BaseData({this.id = 0, this.createdAt, this.updatedAt});
      
        BaseData copyWith({
          int? id,
          String? createdAt,
          String? updatedAt,
        }) {
          return BaseData(
            id: id ?? this.id,
            createdAt: createdAt ?? this.createdAt,
            updatedAt: updatedAt ?? this.updatedAt,
          );
        }
      }
      
      class QcData extends BaseData {
        final String name;
      
        const QcData({
          this.name = "",
          super.id,
          super.createdAt,
          super.updatedAt,
        });
      
        @override
        QcData copyWith({
          String? name,
          int? id,
          String? createdAt,
          String? updatedAt,
        }) {
          return QcData(
            name: name ?? this.name,
            id: id ?? this.id,
            createdAt: createdAt ?? this.createdAt,
            updatedAt: updatedAt ?? this.updatedAt,
          );
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-24
        • 2011-03-03
        • 1970-01-01
        • 1970-01-01
        • 2013-09-06
        • 1970-01-01
        • 2021-09-01
        • 1970-01-01
        相关资源
        最近更新 更多