I recently stumbled upon a package called dart_mappable
, and I feel compelled to share what think about it. If you’re involved in Dart or Flutter development, this might be of particular interest to you.
What Makes It Stand Out?
dart_mappable
is a package that handles JSON serialization and data classes in Dart. However, it’s not just another serialization library. It’s capable of handling complex scenarios that often cause frustration in development.
Here are the features that caught my attention:
-
Comprehensive Functionality: It combines JSON conversion, equality checks, toString, and copyWith into a single package.
-
Handles Complexity Well: It excels at managing generics, polymorphism, and multi-inheritance — areas that are typically challenging.
-
High Flexibility: The serialization process can be customized, new types can be added, and it integrates well with other packages.
-
No compromises: Author’s promise is that it just works, no matter what classes you throw at it. (If you find an unsupported case, you get a cookie from the author ?)
Seeing It in Action
Here’s an example that demonstrates how to use dart_mappable
:
import 'package:dart_mappable/dart_mappable.dart'; part 'example.mapper.dart'; @MappableClass() class Driveable with DriveableMappable { final int wheels; const Driveable({required this.wheels}); } @MappableClass() final class Car extends Driveable with CarMappable { final String model; const Car(this.model, {int wheels = 4}) : super(wheels: wheels); } @MappableClass() final class Man with ManMappable { final String name; final String lastname; final List<Car> cars; const Man({ required this.name, required this.lastname, this.cars = const [], }); } void main() { final teslaS = Car('Model S'); final teslaSPlaid = Car('Model S Plaid'); final teslaX = Car('Model X'); var elonMusk = Man( name: 'Elon', lastname: 'Musk', cars: [ teslaS, teslaSPlaid, teslaX, ], ); // Using copyWith to add a new car elonMusk = elonMusk.copyWith(cars: [...elonMusk.cars, Car('Cybertruck')]); // Serializing to JSON final json = elonMusk.toJson(); print(json); // Deserializing from JSON final deserializedElon = ManMapper.fromJson(json); print(deserializedElon); // Equality check print(elonMusk == deserializedElon); // true }
This example demonstrates how dart_mappable
handles inheritance (Driveable and Car), final classes, lists of objects, and the copyWith functionality with minimal boilerplate code. It showcases serialization, deserialization, equality checking, and easy access to object properties.
The entire generated file is just 325 lines of code.
Performance Considerations
While dart_mappable offers many attractive features, it’s important to note that recent benchmarks have highlighted some performance concerns read this post:
-
Serialization Speed: According to comparative benchmarks, dart_mappable appears to be significantly slower in serialization tasks compared to some popular alternatives like json_serializable, built_value, and freezed.
-
Equality Checks: The equality implementation in dart_mappable has been found to be less performant compared to other solutions.
-
Impact on Different Use Cases: The practical impact of these performance issues may vary depending on your specific use case. For Flutter applications where most of the time is spent on rendering, the impact might be minimal. However, for Dart backends or applications with high serialization workloads, these performance differences could be more noticeable.
It’s worth noting that benchmarks don’t always reflect real-world usage perfectly, and the actual impact on your project may vary. However, if performance is a critical concern for your project, especially in high-load scenarios, you may want to consider alternatives or conduct your own benchmarks with your specific use cases.
Conclusion
Discovering dart_mappable has been an interesting experience. Its ability to handle complex scenarios, support for generics and inheritance, and the compact nature of its generated code are impressive features that could simplify many development tasks.
However, the potential performance trade-offs, particularly in serialization speed and equality checks, are important factors to consider. If you’re working on a Dart or Flutter project, particularly one involving complex data structures but without heavy serialization workloads, dart_mappable might still be a valuable tool.
For projects with high-performance requirements or heavy serialization needs, especially in backend scenarios, you may want to benchmark dart_mappable against other solutions to ensure it meets your specific needs.
I encourage you to try dart_mappable
in your next project if its features align with your requirements. As always in software development, it’s crucial to evaluate tools based on your specific use case. If you do give it a try, I’d be interested to hear your thoughts and experiences!
ссылка на оригинал статьи https://habr.com/ru/articles/834306/
Добавить комментарий