Flutter. Model codegen using build_value package.

Image for post
Image for post

Problems

First, let’s talk about few problems that you could catch as a developer, while creating/updating/deleting properties from you model classes.

{
"by" : "dhouston",
"descendants" : 71,
"id" : 8863,
"kids" : [ 9224, 8917, 8952 ],
"score" : 104,
"time" : 1175714200,
"title" : "My YC app: Dropbox - Throw away your USB drive",
"type" : "story",
"url" : "http://www.getdropbox.com/u/2/screencast.html"
}
Item parseItem(String jsonStr) {
final parsed = json.decode(jsonStr);
return Item.fromJson(parsed);
}
const jsonString =
'{"by":"dhouston","descendants":71,"id":8863,"kids":[8872,8876],"score":104,"time":1175714200,"title":"My YC app: Dropbox - Throw away your USB drive","type":"story","url":"http://www.getdropbox.com/u/2/screencast.html"}';
void main() {
test("parses item json data", () {
expect(parseItem(jsonString).by, 'dhouston');
});
}
Image for post
Image for post
https://www.reddit.com/r/popular/new.json?count=25

It will become a pain in the ass… real headache to maintain changes in such Model structure.

There is a great solution to solve that — build_value

At first, we will need to add few packages to our project:

  • built_collection
  • built_value_generator
  • build_runner
dependencies:
...
built_value: ^7.1.0
built_collection: ^4.3.2
dev_dependencies:
built_value_generator: ^7.1.0
build_runner: ^1.9.0

To simplify “describing” you can use code snippet plugin in your IDE.

Step 1

In a someName.dart file

import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
import 'package:built_collection/built_collection.dart';

part 'someName.g.dart';
Image for post
Image for post

Step 2

Run this command in ROOT path of your project:

flutter packages pub run build_runner watch

Back to business

Now, when you know how it work, let’s get back to explaining previous “complex” code fragment:

Image for post
Image for post
flutter packages pub run build_runner watch
Image for post
Image for post

Generating serializers

Next, we need to generate serializers. The process is mostly the same.

Image for post
Image for post
flutter packages pub run build_runner watch

Let’s test it

Create a function that will fetch Reddit data and parse it:

Image for post
Image for post

P.S. Building new object

Because your classes are immutable you can create you new object like this:

new Post((b) => b
..author = 'yashsaini9127'
..url = 'https://some-url'
..title = 'What will you choose'
..ups = 1);

And that’s it!

You can add/update/delete properties from your classes and build_runner will automatically re-generate it on a fly 👍

Image for post
Image for post

Java, Spring, Node.js, AdonisJs, React.js and Flutter developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store