List<Model> 不是领域模型类型

List<Model> is not a realm model type

我是 Realm 的新手,我想使用它 with Flutter (Dart),但是我在使用此命令生成 g 文件 时遇到问题

flutter pub run realm generate

这是终端错误:

➜  app-android git:(develop) ✗ flutter pub run realm generate
[INFO] Generating build script...
[INFO] Generating build script completed, took 763ms

[INFO] Initializing inputs
[INFO] Reading cached asset graph...
[INFO] Reading cached asset graph completed, took 132ms

[INFO] Checking for updates since last build...
[INFO] Checking for updates since last build completed, took 1.3s

[INFO] Running build...
[INFO] Running build completed, took 28ms

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 58ms

[SEVERE] realm:realm_generator on lib/Models/FavoritesModel/favorites_model.dart (cached):

Not a realm type

in: package:test_app/Models/FavoritesModel/favorites_model.dart:11:14
╷
6   │ @RealmModel()
7   │ class _FavoritesModel {
    │       ━━━━━━━━━━━━━━━ in realm model for 'FavoritesModel'
... │
11  │   late final List<AudiosArticleModel> data;
    │              ^^^^^^^^^^^^^^^^^^^^^^^^ List<AudiosArticleModel> is not a realm model type
    ╵
Remove the invalid field or add an @Ignored annotation on 'data'.

[SEVERE] Failed after 110ms

这是我的 favorites_model.dart 文件:

import 'package:realm/realm.dart';
import '../AudiosArticleModel/audios_article_model.dart';
part 'favorites_model.g.dart';

@RealmModel()
class _FavoritesModel {

  late String title;
  late String author;
  late final List<AudiosArticleModel> data;
}

有人知道问题出在哪里吗?我已经使用相同的命令成功生成了 AudiosArticle 模型。

audios_article_model.dart

import 'package:realm/realm.dart';
part 'audios_article_model.g.dart';

@RealmModel()
class _AudiosArticleModel {
  late String link;
  late String title;
  late String author;
  late String hour;
  late String minute;
  late String second;
}

audios_article_model.g.dart

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'audios_article_model.dart';

// **************************************************************************
// RealmObjectGenerator
// **************************************************************************

class AudiosArticleModel extends _AudiosArticleModel with RealmObject {
  AudiosArticleModel(
    String link,
    String title,
    String author,
    String hour,
    String minute,
    String second,
  ) {
    this.link = link;
    this.title = title;
    this.author = author;
    this.hour = hour;
    this.minute = minute;
    this.second = second;
  }

  AudiosArticleModel._();

  @override
  String get link => RealmObject.get<String>(this, 'link') as String;
  @override
  set link(String value) => RealmObject.set(this, 'link', value);

  @override
  String get title => RealmObject.get<String>(this, 'title') as String;
  @override
  set title(String value) => RealmObject.set(this, 'title', value);

  @override
  String get author => RealmObject.get<String>(this, 'author') as String;
  @override
  set author(String value) => RealmObject.set(this, 'author', value);

  @override
  String get hour => RealmObject.get<String>(this, 'hour') as String;
  @override
  set hour(String value) => RealmObject.set(this, 'hour', value);

  @override
  String get minute => RealmObject.get<String>(this, 'minute') as String;
  @override
  set minute(String value) => RealmObject.set(this, 'minute', value);

  @override
  String get second => RealmObject.get<String>(this, 'second') as String;
  @override
  set second(String value) => RealmObject.set(this, 'second', value);

  static SchemaObject get schema => _schema ??= _initSchema();
  static SchemaObject? _schema;
  static SchemaObject _initSchema() {
   RealmObject.registerFactory(AudiosArticleModel._);
   return const SchemaObject(AudiosArticleModel, [
    SchemaProperty('link', RealmPropertyType.string),
    SchemaProperty('title', RealmPropertyType.string),
    SchemaProperty('author', RealmPropertyType.string),
    SchemaProperty('hour', RealmPropertyType.string),
    SchemaProperty('minute', RealmPropertyType.string),
    SchemaProperty('second', RealmPropertyType.string),
     ]);
  }
}

我发现了问题:模型 class 是私有的,因为有 _ 前缀。

解决方案是使用 $ 前缀代替所有项目 public。

audios_article_model.dart

import 'package:realm/realm.dart';
part 'audios_article_model.g.dart';

@RealmModel()
class $AudiosArticleModel {
  late String link;
  late String title;
  late String author;
  late String hour;
  late String minute;
  late String second;
}

当然需要生成g文件