Dart RPC and object hierarchies

202 views Asked by At

This is probably a really basic question about REST APIs. I just started reading the docs and don't see how to implement an API for an object/model that has sub-objects. Instead there is only a description of objects with primitives only (int, string, etc).

Is there an example with an object hierarchy? Or maybe the server model just uses IDs in class definitions to refer to sub-objects, like how Google's NDB does it?

2

There are 2 answers

1
Peter StJ On

From docs:

Currently supported types for the public fields are int, double, bool, String, DateTime, List, Map, and another message class.

This means you can create the structure you need, the only requirement is that all objects in it must be constructed of in that particular way.

An example:

class Person {
  String name;
  int age
}

class Address {
  String city;
  String street;
}

class School {
  List<Person> staff;
  Person headmaster;
  List<Person> students;
  Address address;
}

One limitation I did not like much is that you cannot use inheritance in these classes, but then again the whole point of the rpc package is to create an API that can be consumed by any client, not just dart code.

0
sgjesse On

If you look in the messages used for testing the package there are examples using sub-messages, e.g. TestMessage1.

For more examples you can look at the dart_services project, which is the backend for dartpad. It has its current api here and message definitions here. dart_services does not use direct nested messages, but nested lists of other messages.