Nitrite/Flutter: Getting an error creating an ObjectRepository #51
-
I'm new to Nitrite and Flutter, so I'm not sure if I'm missing something and could use some help. I'm trying to follow the docs to create an ObjectRepository for a Todo Object with an embedded Properties Object to understand how Nitrite works but hitting errors I've tried both the code generator and doing it manually with an EntityDecorator. The Todo Object has 1 field, the embedded final nonNull Properties object. Using the code generator, I annotated Todo with Entity and Convertable and Properties with Convertable. It looks like it breaks on getting the NitriteMapper.pluginManager. Also, the generated no2 files for Todo and Properties have errors saying "type dynamic can't be assigned to the parameter" when it tries to instantiate the objects. Using EntityDecorator, it either says the errors above or when I try to cast the dynamic "document[fieldName]" to the specific type, it errors with the error at the bottom of this: EntityDecorator for the Todo Object
2 EntityConverters, 1 for Todo and the other for Properties and registered both using registerEntityConverter()
Then I try to create the TodoRepository using the TodoDecorator From there, it goes through some nitrite checks, confirms there is no TodosRepository and goes to create it. As part of the validation it goes into the fromDocument method for my TodoConverter. Then when it does tryConvert on the propsDoc, I get this error from nitrite's validation_utils: The debugger says the document passed into FromDocument is an empty map and the property it's trying to convert is null, which makes sense because the document and database are empty, but why is it trying to convert when it's supposed to be creating a repository? It feels like I missed a step but don't know what Any help would be appreciated |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 4 replies
-
Thanks for trying out Nitrite. Here is the code that would work in your case. import 'dart:io';
import 'package:faker/faker.dart';
import 'package:nitrite/nitrite.dart';
import 'package:nitrite_hive_adapter/nitrite_hive_adapter.dart';
part 'github_test.no2.dart'; // <-- generated file
@Convertable()
@Entity()
class Todo with _$TodoEntityMixin {
@Id()
final String id;
final Properties? properties;
Todo({
required this.id,
this.properties,
});
@override
String toString() {
return 'Todo{id: $id, properties: $properties}';
}
}
@Convertable()
class Properties {
final String id;
final Map<String, dynamic> locations;
Properties({
required this.id,
required this.locations,
});
@override
String toString() {
return 'Properties{id: $id, locations: $locations}';
}
}
void main() async {
var faker = Faker();
var dbPath = '${Directory.current.path}/db/${faker.guid.guid()}';
var storeModule =
HiveModule.withConfig().crashRecovery(true).path(dbPath).build();
var db = await Nitrite.builder()
.loadModule(storeModule)
.registerEntityConverter(TodoConverter()) // <-- generated converter
.registerEntityConverter(PropertiesConverter()) // <-- generated converter
.openOrCreate();
var todoRepository = await db.getRepository<Todo>();
var todo = Todo(
id: faker.guid.guid(),
properties: Properties(
id: faker.guid.guid(),
locations: {
'location1': 'location1',
'location2': 'location2',
},
),
);
await todoRepository.insert(todo);
var todoById = await todoRepository.getById(todo.id);
print(todoById);
} Notice the |
Beta Was this translation helpful? Give feedback.
-
Documentation has been updated here. |
Beta Was this translation helpful? Give feedback.
-
Thank you that fixed me up! I have one in my Properties object and when I try to insert a todo, it breaks on converting the enum to a document. |
Beta Was this translation helpful? Give feedback.
-
I ended up trying a customer converter for my enum:
but I'm still getting this error
|
Beta Was this translation helpful? Give feedback.
-
Also, if I have my id in Properties, is this the correct implementation to have Nitrite use it for EntityId
My error seems to be on line 154 in repository_operations.dart. It can't find the embedded id because it says idValue is null |
Beta Was this translation helpful? Give feedback.
Thanks for trying out Nitrite. Here is the code that would work in your case.