Decoders are a new concept introduced in Nylo which allows you to decode data into objects or classes. You'll likely use decoders when dealing with the networking class or if want to use the api helper in Nylo.

By default, the location for decoders is lib/config/decoders.dart

The decoders.dart file will contain two variables:

Model decoders

Model decoders are new in Nylo, they provide a way for you to morph data payloads into model representations.

The network() helper method will use the modelDecoders variable inside your config/decoders.dart file to determine which decoder to use.

Here's an example.

Here's how the network helper uses modelDecoders.

class ApiService extends BaseApiService {
  ApiService({BuildContext? buildContext}) : super(buildContext);

  String get baseUrl => "";

  Future<User?> fetchUsers() async {
    return await network<User>(
        request: (request) => request.get("/users"),

The fetchUsers method will automatically decode the payload from the request into a User.

How does this work?

You have a User class like the below.

class User {
  String? name;
  String? email;

  User.fromJson(dynamic data) { = data['name']; = data['email'];

  toJson()  => {

You can see from the above that this class has a fromJson method which provides us with a way to initialize the class.

We can initialize this class by calling the below method.

User user = User.fromJson({'name': 'Anthony', 'email': ''});

Now, to set up our decoders, we have do the following.

File: config/decoders.dart

final modelDecoders = {
  List<User>: (data) => List.from(data).map((json) => User.fromJson(json)).toList(),

  User: (data) => User.fromJson(data),

  // ...

In the modelDecoders file, we need to provide the Type as the key and handle the morph in the value like the above example.

The data argument will contain the payload from the API request.

API decoders

API decoders are used when calling the api helper method.

loadUser() async {
    User user = await api<ApiService>((request) => request.fetchUser());

The api helper will match the correct API Service using generics so you can call the below helper to access your service.

await api<MyService>((request) => request.callMyMethod());

Before using the api helper, you will need to first add your API Service into lib/config/decoders.dart > apiDecoders.

final Map<Type, dynamic> apiDecoders = {
  ApiService: ApiService(),

  // ...