简体   繁体   中英

Flutter ObjectBox - Can't generate DB model with custom class

Following the NoSQL Database Storage | Flutter ObjectBox (vs Sqlite, Sqflite, Hive) , I am trying to setup an ObjectBox database with multiple classes. I currently have the following classes,

Exercise.dart

import 'package:objectbox/objectbox.dart';

@Entity()
@Sync()
class Exercise {
  int? id;
  final String? name;
  final String? target;
  final String? bodyPart;
  final String? equipment;
  final String? gifUrl;

   Exercise({
    this.id = 0,
    required this.name,
    required this.target,
    required this.bodyPart,
    required this.equipment,
    required this.gifUrl,
  });

  factory Exercise.fromJson(Map<String, dynamic> json) {
    return Exercise(
        id: int.tryParse(json['id'].toString()),
        name: json["name"].toString(),
        target: json["target"].toString(),
        bodyPart: json["bodyPart"].toString(),
        equipment: json["equipment"].toString(),
        gifUrl: json["gifUrl"].toString());
  }
}

and
Set.dart

import 'package:objectbox/objectbox.dart';
import 'package:pump_that_iron/models/exercise.dart';

@Entity()
@Sync()
class Set {
  int? id;
  final Exercise? exercise;
  final int? repetitions;

  Set({
    this.id = 0,
    required this.exercise,
    required this.repetitions,
  });
}

From these classes I am trying to build a model class using ObjectBox and the flutter pub run build_runner build command. However, whenever that command is run, I get the following error,

Cannot use the default constructor of 'Set': don't know how to initialize param exercise - no such property.

as well as

[WARNING] objectbox_generator:resolver on lib/models/set.dart: skipping property 'exercise' in entity 'Set', as it has an unsupported type: 'Exercise?'

Initially, when running the build command with the Exercise.dart being the only present class, it is successful. However, as soon as I add the Set.dart file containing the Exercise class as a variable , I am no longer able to build. Another thing to note, is that when the Exercice variable is omitted from the Set class, it runs successfully.

I found the following How initialize Objectbox entity with custom type? SO thread with a similar problem and tried to replicate the steps by,

  1. Adding a getExercise getter in the Set.dart class and adding it to the constructor
Set({
    this.id = 0,
    this.exercise = getExercise,
    required this.repetitions,
  });

  get getExercise => Exercise(
      name: "name",
      target: "target",
      bodyPart: "bodyPart",
      equipment: "equipment",
      gifUrl: "gifUrl");
  1. This required me to make the Exercise.dart constructor constant, and making the following changes
class Exercise {
  @Id(assignable: true)
  final int? id;
  ...

  const Exercise({
    this.id = 0,
    ...
  });

This is now giving me a The default value of an optional parameter must be constant. error when trying to set the exercise variable at this.exercise = getExercise . I tried following the instructions from another SO thread Default values of an optional parameter must be constant , but I am still having issues.

I was wondering if anybody could help me set up my database and explain what is going wrong. Any help is appreciated.

Thank you.

ObjectBox just supports storing some base types by default . For custom types you have to either map them to a supported type , or I assume what you are looking for in this case create a relation to an @Entity class:

@Entity()
@Sync()
class Set {
  int? id;
  final exercise = ToOne<Exercise>();
  final int? repetitions;
}

// To change the ToOne target:
set.exercise.target = Exercise();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM