继承flutter中的构造函数和工厂

问题描述 投票:0回答:1

我正在学习 flutter,我正在尝试将请求结果转换为更方便的对象,因此我创建了响应的基本模式(不应该随时实例化):

class BaseModel {
  final String id;
  final DateTime createdAt;
  final DateTime updatedAt;
  final String databaseId;
  final String collectionId;

  BaseModel(
      {required this.id,
      required this.createdAt,
      required this.updatedAt,
      required this.databaseId,
      required this.collectionId});

  factory BaseModel.fromMap(Map<String, dynamic> map) {
    return BaseModel(
        id: map['\$id'].toString(),
        collectionId: map['\$collectionId'].toString(),
        createdAt: DateTime.tryParse(map['\$createdAt'].toString())!,
        updatedAt: DateTime.tryParse(map['\$updatedAt'].toString())!,
        databaseId: map['\$databaseId'].toString());
  }
}

我想使用这个基础来创建我需要的所有其他对象,但我觉得每次扩展类时我都被迫编写一些无用的代码:

class ProfilePictureModel extends BaseModel {
  final String userId;
  final Uri pfp;

  ProfilePictureModel(
      {required super.id,
      required super.createdAt,
      required super.updatedAt,
      required super.databaseId,
      required super.collectionId,
      // everything above that has been written already in super class
      required this.userId,
      required this.pfp});

  factory ProfilePictureModel.fromMap(Map<String, dynamic> map) {
    return ProfilePictureModel(
      id: map['\$id'].toString(),
      collectionId: map['\$collectionId'].toString(),
      createdAt: DateTime.tryParse(map['\$createdAt'].toString())!,
      updatedAt: DateTime.tryParse(map['\$updatedAt'].toString())!,
      databaseId: map['\$databaseId'].toString(),
      // everything above that has been written already in super class
      userId: map['user_id'].toString(),
      pfp: Uri.dataFromString(map['pfp_url'].toString()),
    );
  }
}

最终我的问题是:在这种情况下如何利用超级构造函数和工厂?我觉得设置

BaseModel
abstract
很困难。也许我走错路了?已经谢谢了,偷看!

flutter dart inheritance super
1个回答
0
投票

您可以直接使用 BaseModel 作为对象,而不是将其用作超类。

class BaseModel {
  final String id;
  final DateTime? createdAt;
  final DateTime? updatedAt;
  final String databaseId;
  final String collectionId;

  BaseModel({
    required this.id,
    required this.createdAt,
    required this.updatedAt,
    required this.databaseId,
    required this.collectionId,
  });

  factory BaseModel.fromMap(Map<String, dynamic> map) {
    return BaseModel(
      id: map['id'] as String,
      createdAt: DateTime.tryParse(map['createdAt'].toString()),
      updatedAt: DateTime.tryParse(map['updatedAt'].toString()),
      databaseId: map['databaseId'] as String,
      collectionId: map['collectionId'] as String,
    );
  }
}

class ProfilePictureModel {
  final String userId;
  final String pfp;
  final BaseModel baseModel;

  ProfilePictureModel({
    required this.userId,
    required this.pfp,
    required this.baseModel,
  });


  factory ProfilePictureModel.fromMap(Map<String, dynamic> map) {
    return ProfilePictureModel(
      userId: map['userId'] as String,
      pfp: map['pfp'] as String,
      baseModel: BaseModel.fromMap(map['baseModel'] as Map<String, dynamic>),
    );
  }
}

© www.soinside.com 2019 - 2024. All rights reserved.