What is _$? #Dart #Flutter

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."

stackoverflow.com/a/62179466/506…
It finally made sense.

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`:

docs.flutter.dev/development/da…

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
 

Keep Current with Rohan Taneja

Rohan Taneja Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @rohantaneja_

2 Jan
I love @FlutterDev 💙
I'm super into @OfficialFPL too

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.
Read 8 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(