dart 我无法使用`isar`和`freezed`创建模型

deikduxw  于 2024-01-04  发布在  其他
关注(0)|答案(1)|浏览(99)

我想创建一个如下所示的模型,但没有生成isar.g.文件。

import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:isar/isar.dart';

part 'model.freezed.dart';
part 'model.g.dart';

@freezed
@Collection(ignore: {'copyWith'})
class Model with _$Model {
  const factory Model({
    required Id id,
    required String name,
  }) = _Model;
  const Model._();
}

字符串
错误类型:

Target of URI hasn't been generated: 'package:my_app/entity/model.g.dart'.
Try running the generator that will generate the file referenced by the URI.


如何使freezedisar共存并一起生成文件?

z9smfwbn

z9smfwbn1#

在pubspec.yaml文件中,使用最新的isar和freezed包,

isar: ^4.0.0-dev.14
isar_flutter_libs: ^4.0.0-dev.14
freezed_annotation: ^2.4.1

字符串
在您的模型中。dart类,更改Id id 👉 int id

import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:isar/isar.dart';

part 'model.freezed.dart';
part 'model.g.dart';

@freezed
@Collection(ignore: {'copyWith'})
class Model with _$Model {
  const factory Model({
    // required Id id, //Don't use this❌
    required int id,   //Use this ✅
    required String name,
  }) = _Model;
  const Model._();
}


然后在您的终端中运行此命令

dart run build_runner build --delete-conflicting-outputs


这将同时生成model.freezed.dart和model.g.dart文件。
对于ISAR v4中的中断,请将其读取为isar 4.0.0-dev.14 changelog

相关问题