Support Topics Documentation Slack YouTube Blog

1:N Relations in Custom Class Model

Hi,

I Have a Post Model with a 1:N Relation to Comments Model.

To avoid errors in reflection package I have to define the Post custom class like this:

@reflector
class Post {
String title;
List<dynamic> comments;
}

When the correct definition will be:

@reflector
class Post {
String title;
List<Comment> comments;
}

If I have several relationship attributes it causes me to have to convert the data obtained from the Backendless API all the time.

Would it be possible to correct this?

Regards,
Dani

Hi @Dani_Sevilla

There are some limitations for the related object retrieval. Since Dart cannot reflect the type of the list in the runtime, there are two ways to declare the collection of related objects inside the custom parent class.

1.You can declare the collection of child objects as List<dynamic>. It is a simple way:

@reflector
class ParentTable {
  List<dynamic> children;  
}

2.Or you can declare children as private field List<ChildTable> and create public setter&getter that will cast the list to the desired type:

@reflector
class ParentTable {
  List<ChildTable> _children;  
  set children(List value) => _children = value.cast<ChildTable>();  
  get children => _children;
}

Best Regards,
Maksym

Ok thanks @Maksym_Khobotin

I didn’t know the second option, I will use it.

Regards,
Dani

We will update the documentation with this example.
Thank you for pointing this out!

Regards,
Maksym