DEV Community

Cover image for Using Dart Extensions to Decouple Mappers in Big Flutter Projects
Ibekason Alexander
Ibekason Alexander

Posted on

Using Dart Extensions to Decouple Mappers in Big Flutter Projects

Working on big flutter projects introduces lots of bottlenecks. The general method of creating entity mappers and json converters in entity classes are no longer the best approach when working on big projects.

Typically, an entity class in big flutter projects may need to have several mappers to other classes as well as Json converters and/or other types of data. This introduces readability issues and presents difficulty when trying to refractor existing code.

This is how a typical user class in many flutter project looks like

We can see lots of things are going on in this class. Removing or adding new fields may lead to lots of refactoring in this class and/or other classes.

Introduction Dart Extensions
From dart docs, Extension methods, introduced in Dart 2.7, are a way to add functionality to existing libraries.

Dart extensions solves this problem by decoupling mappers and converters to a separate responsible class.

Changes in User properties that affects mappers and converters will only be refactored in the decoupled mappers.

Create a UserMapper class as this

Our User Entity is now simplified as this

With this better approach, changes will only be effected in our various mappers and will ensure a cleaner error-prone entity classes.

Want to know more about Dart Extensions?

Check out this dart documentation

Thank you for your time :)

Top comments (0)