What are Serializable Objects?

These objects are responsible for encoding and decoding the payload of a request. To use this class, define our subclass and override the asMap() and readFromMap() methods.

class Read extends Serializable {

String title;

String author;

int year; @override

Map<String, dynamic> asMap() => {

'title': title,

'author': author,

'year': year,

} @override

void readFromMap(Map<String, dynamic> requestBody) {

title = requestBody['title'] as String;

author = requestBody['author'] as String;

year = requestBody['year'] as int;

}

}

So the asMap() method takes in the instance members of Read and returns a Map structure, useful when returning JSON responses.

readFromMap() is used when extracting the request payload keys into our instance properties. This allows us to therefore bind our instance to the body payload parameter that comes in our POST request method:

class ReadsController extends ResourceController {

...

...

@Operation.post()

Future<Response> createNewRead(@Bind.body() Read body) async {

...

}

...

...

}

See how this works in further detail and how we integrate a fully functional PostgreSQL database in the full video.

→ Get the source code