【问题标题】:What is the use of extending model class with Equatable in flutter在flutter中用Equatable扩展模型类有什么用
【发布时间】:2022-09-28 15:16:21
【问题描述】:
class PushOtpResponse extends Equatable {
  final bool? isSuccess;
  final String? message;

  const PushOtpResponse({this.isSuccess, this.message});

  factory PushOtpResponse.fromJson(Map<String, dynamic> json) {
    return PushOtpResponse(
      isSuccess: json[\'isSuccess\'] as bool?,
      message: json[\'message\'] as String?,
    );
  }

  Map<String, dynamic> toJson() => {
        \'isSuccess\': isSuccess,
        \'message\': message,
      };

  @override
  bool get stringify => true;

  @override
  List<Object?> get props => [isSuccess, message];
}

上面给出的模型类是使用 json 来创建 VSCode 的 dart 扩展。有一个使用 equatable 扩展类的选项。那么,用 equatable 扩展模型类有什么用吗? \'stringify\' 在这里有什么帮助吗?

  • 它只是触发另一个状态,无论是 Bloc 还是 Riverpod。

标签: flutter dart


【解决方案1】:

我建议您阅读 Equatable 的文档。 Equatable 不是 Flutter 的默认类,无论是谁将那个代码添加到你的项目中,一定是故意包含了这个包

https://pub.dev/packages/equatable

【讨论】:

    【解决方案2】:

    Equatable 覆盖 == 和 hashCode 而无需生成代码,甚至不需要自己编写代码。

    使用 Equatable,您可以

        final resp = PushOtpResponse(
          isSuccess: true,
          message: 'Test',
        );
        final resp2 = PushOtpResponse(
          isSuccess: true,
          message: 'Test',
        );
    
        resp == resp2; // True with Equatable, False without
    

    有关更多详细信息,您可以阅读公平的文档 https://pub.dev/packages/equatable

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      • 2021-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-04
      相关资源
      最近更新 更多