Back in 2020, I had been working with Flutter/Dart for a while, but I had zero clue about the concept of generated code.
Here’s what followed ⬇️
I saw this strange _$ syntax everywhere.
I knew that a prefixed underscore is Dart’s way of saying that a certain thing is private but I wasn’t sure if the _ in _$ also meant the same thing.
It felt alien to see a $ sign in class/method names.
One day, I saw some code on @FlutterComm’s Slack, that used _$.
It took some courage to DM the author of that code and ask what that syntax was because it seemed that I was the only one who didn’t understand it, which is why I shied away from asking on the public channel 🙊
That person was kind enough to clearly explain that adding a $ sign to class/method name is intended to denote that its implementation comes from auto-generated code.
The _ in front of the $ sign still represents “private”, just like anywhere else in Dart.
$, as also explained on SO:
"...is just another letter to the language, but it's reserved for generated code. That allows code generators to (largely) avoid worrying about naming conflicts as long as they put a `$` somewhere in the names they create."
The next part was to figure out how do I benefit from this generated code and how was it actually being generated?
So I searched for “generate code dart”. The top results mentioned “build_runner” and “source_gen”.
I had read about `build_runner` before but never really understood it because not understanding the `_$` part put me off.
But now with `_$` out of the way, I felt confident that I could understand this concept.
Long story short:
`build_runner` is a package that generates Dart code.
`source_gen` is a package that helps define the logic for generating Dart code.
“Give it to me in English”?
- You write 1️⃣ line of code.
- You run `build_runner` and get 1️⃣0️⃣0️⃣ lines of code related to that 1️⃣ line magically.
- `source_gen` is the magician 🪄. It helps define the logic for getting the 1️⃣0️⃣0️⃣ lines from that 1️⃣ line.
For most of us who are just building Flutter apps for end-users, we may only need `build_runner`.
We can still automate writing some repetitive code by using `source_gen` if needed but for most cases, if we only want to consume some packages, `build_runner` is enough:
I then followed a tutorial to try these^ commands and it started to make more sense.
Then gradually I got acquainted with packages like `json_serializable` and `freezed` that heavily use code generation and make our lives as devs easier.
I now use these 2 in my apps everyday.
“Where do I go from here?”
I’d suggest starting with this codelab from the Flutter docs that walks you through using `json_serializable` using `build_runner`:
It’s easy to follow and you’ll clearly see the benefits of using generated code by its end.
That's it! ⭐️
So just like Rohan from 2020, if you found the concept of generated code overwhelming, I hope this thread made it slightly easier for you to understand it 🙌
• • •
Missing some Tweet in this thread? You can try to
force a refresh
So I decided to combine my love for both of them to solve some FPL pain points by building @FantasyPLApp ⬇️
This is (finally!) a side-project that I’ve actually shipped to real users and not just my friends (or parents, lol). I’ve learned a ton building this app. It’s far from done but it brings me immense joy to see this being useful for a few already 🙌
FPL is short for Fantasy Premier League. It’s a game where players score goals in the real world and you get virtual points if you had those players on your team and hence compete with your friends’ teams. Sounds silly but can get very intense 😛 #FPLCommunity is huge on Twitter.