dart 在@freezed注解类上使用comparable会导致编译时错误

imzjd6km  于 2023-02-14  发布在  其他
关注(0)|答案(2)|浏览(186)
    • bounty将在2小时后过期**。回答此问题可获得+50声望奖励。Rahul希望引起更多人关注此问题。

我必须对使用@freezed注解创建的类的项进行排序。我已将Comparable接口添加到签名中。这将导致以下编译时异常:
错误(Xcode):库/模型/应用程序语言.已冻结. dart:143:7:错误:非抽象类"$ AppLanguage"缺少这些成员的实现:
无法为模拟器生成应用程序。

// app_language.dart

import 'package:freezed_annotation/freezed_annotation.dart';

part 'app_language.freezed.dart';
part 'app_language.g.dart';

@freezed
class AppLanguage with _$AppLanguage implements Comparable<AppLanguage> {
  const factory AppLanguage({
    @Default("en") String id,
    String? name,
    String? msg,
    @Default([0, 0, 0, 0]) List<int> color,
    @Default(false) bool rtl,
  }) = _AppLanguage;

  factory AppLanguage.fromJson(Map<String, dynamic> json) =>
      _$AppLanguageFromJson(json);

  @override
  int compareTo(AppLanguage other) {
    if (rtl == other.rtl) {
      if (name != null && other.name != null) {
        return name!.compareTo(other.name!);
      } else if (name == null && other.name == null) {
        return 0;
      } else if (name != null) {
        return 1;
      } else {
        return -1;
      }
    } else {
      return rtl ? 1 : -1;
    }
  }
}
  • pubspec. yaml *(代码片段)
dependencies:
  freezed: ^2.3.2
  freezed_annotation: ^2.2.0
dev_dependencies:
  build_runner: ^2.3.3
  json_serializable: ^6.6.0

用法

// here value is list of AppLanguages and I am returning another sorted list.
return <AppLanguage>[...value]..sort();

我如何解决编译错误?任何帮助将不胜感激.
现在,我已经添加了单独的比较器,以便它可以工作。但让模型类实现比较器是可取的。

import 'package:freezed_annotation/freezed_annotation.dart';

import '../localization/localization.dart';

part 'app_language.freezed.dart';
part 'app_language.g.dart';

@freezed
class AppLanguage with _$AppLanguage {
  const factory AppLanguage({
    @Default(localeDefault) String id,
    String? name,
    String? msg,
    @Default([0, 0, 0, 0]) List<int> color,
    @Default(false) bool rtl,
  }) = _AppLanguage;

  factory AppLanguage.fromJson(Map<String, dynamic> json) =>
      _$AppLanguageFromJson(json);

  // @override
  // // ignore: library_private_types_in_public_api
  // int compareTo(_$_AppLanguage other) {
  //   if (rtl == other.rtl) {
  //     if (name != null && other.name != null) {
  //       return name!.compareTo(other.name!);
  //     } else if (name == null && other.name == null) {
  //       return 0;
  //     } else if (name != null) {
  //       return 1;
  //     } else {
  //       return -1;
  //     }
  //   } else {
  //     return rtl ? 1 : -1;
  //   }
  // }
}

int appLanguageComparator(AppLanguage a, AppLanguage b) {
  if (a.rtl == b.rtl) {
    final aName = a.name;
    final bName = b.name;
    if (aName != null && bName != null) {
      return aName.compareTo(bName);
    } else if (aName == null && bName == null) {
      return 0;
    } else if (aName != null) {
      return 1;
    } else {
      return -1;
    }
  } else {
    return a.rtl ? 1 : -1;
  }
}

用法:

return <AppLanguage>[...value]..sort(appLanguageComparator);
nkcskrwz

nkcskrwz1#

对于将来会遇到同样问题的其他人来说,为了给freezed类添加自定义方法/getter等,你需要提供一个空的const构造函数。

@freezed
class Person with _$Person {
  const factory Person(String name, {int? age}) = _Person;

  void method() {
    print('hello world');
  }
}

此实现将引发编译错误,为了使其正常工作,您需要执行以下操作:

@freezed
class Person with _$Person {
  // Added constructor. Must not have any parameter
  const Person._();

  const factory Person(String name, {int? age}) = _Person;

  void method() {
    print('hello world');
  }
}

请记住,构造函数必须是空构造函数。

wljmcqd8

wljmcqd82#

您必须使用Comparable<AppLanguage>扩展该类,并且应该使用参数AppLanguage覆盖compareTo,如下所示

import 'package:freezed_annotation/freezed_annotation.dart';

import '../localization/localization.dart';

part 'app_language.freezed.dart';
part 'app_language.g.dart';

@freezed
class AppLanguage extends Comparable<AppLanguage> with _$AppLanguage {
  const factory AppLanguage({
    @Default(localeDefault) String id,
    String? name,
    String? msg,
    @Default([0, 0, 0, 0]) List<int> color,
    @Default(false) bool rtl,
  }) = _AppLanguage;

  factory AppLanguage.fromJson(Map<String, dynamic> json) =>
      _$AppLanguageFromJson(json);

  
 @override
 int compareTo(AppLanguage other) {
   if (rtl == other.rtl) {
     if (name != null && other.name != null) {
       return name!.compareTo(other.name!);
     } else if (name == null && other.name == null) {
       return 0;
     } else if (name != null) {
       return 1;
     } else {
       return -1;
     }
   } else {
     return rtl ? 1 : -1;
   }
 }
}

相关问题