I cannot create a model using `isar` and `freezed`

427 views Asked by At

I want to create a model like the following, but the .g. file for isar is not being generated.

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._();
}

error:

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.

How can I make freezed and isar coexist and generate files together?

1

There are 1 answers

1
abdullah_bd On BEST ANSWER

In your pubspec.yaml file, use latest isar and freezed package,

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

In your model.dart class, change 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._();
}

Then run this command in your terminal

dart run build_runner build --delete-conflicting-outputs

This will generate both model.freezed.dart, model.g.dart file together.

For breaking in ISAR v4, read it isar 4.0.0-dev.14 changelog.