Issue
I'm new to Realm, and I want to use it with Flutter (Dart), but I have a problem when generating g file with this command
flutter pub run realm generate
This is the error in terminal:
➜ 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
Here is my favorites_model.dart file:
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;
}
Does someone know what's the problem ? I already generated successfully the AudiosArticle Model with the same command.
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),
]);
}
}
Solution
I found the problem: the model class was private because of _ prefix.
The solution is to use $ prefix instead to make it public for all project.
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;
}
Of course you need to generate g file.
Answered By - swifthing
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.