Built Value Tutorial for Dart & Flutter

Data classes, immutability, simple serialization – this is what Dart does NOT offer right off the bat. While Dart is surely one of the better languages with regards to its simplicity, it currently lacks the support for value types.

When you compare two objects of the same type with identical contents, they will still turn out to be not equal – by default, Dart checks for referential equality, not value equality. Making a copy of an object is also painful – you have so much code to write… And serialization? Oh, even more repetitive code for you!

Well, not with built_value library. It will save you so much time by allowing you to write good quality code without all the repetition.

👉Get the code from this tutorial👈

 

Dart built_value snippets

 

 

Icons made by Becris from www.flaticon.com is licensed by CC 3.0 BY

Leave a comment


This site uses Akismet to reduce spam. Learn how your comment data is processed.


João Vicente (BRA)

1 month ago

Hi Matej, congratulations! Excellent material about ‘built_value’!

I have a question.

How could i implement a named constructor like this: “MyClass.fromMap(String data) {…}’

Look at the example below, i have a custom initialization to the field ‘userRole’:

Home.fromMap(Map data) {
this.companyCode = data[‘companyCode’];
this.companyName = data[‘companyName’];
this.companyLogo = data[‘companyLogo’];
this.userRole = UserRole.valueOf(data[‘userEmail’]); // data[‘userEmail’] returns the userRole string
}

TIA

Matej Rešetár

1 month ago

Hello!
I think you can do something like in this: https://gist.github.com/ResoDev/b82566e26a3b5aae8daa659a0c82bd3b

Sebastian

4 weeks ago

Here is the intellj LiveTemplets version for the snippets:
https://gist.github.com/SebastianKlaiber/a4ca60d2b082185edc97fff6763e6a03

Matej Rešetár

4 weeks ago

Thank you for posting them!

Development made simple

We're here to help you. If you have any questions, let us know.

Copyright 2017 - 2018 Reso Coder © All Rights Reserved