Using --dart-define, a 🧵

When we're developing Flutter apps we can have two issues:

1⃣ We want to add `flavors` for Flutter Web, but there's no out-of-the-box way of doing it

2⃣ Our app needs an API key or configuration that we don't want to commit

In pure Dart, this can be easily achievable with the args package:

We are able to stipulate what are the flags and arguments we need to use, such as:

`dart run bin/converter.dart --verbose --out-path="/out" data.json`

This will:
1⃣ Run the converter.dart script
2⃣ With the flag verbose
3⃣ Setting an `out-path` of `/out`
4⃣ Accepting as a last argument the file `data.json`

However, the same cannot be applied to Flutter.


Because we have no direct control over

`flutter run`

Thankfully Dart provides us a solution 🚀

Using --dart-define in our `flutter run` command we can specify a variable name and its value so that our Dart or Flutter application can use it 🙌

In this example, we define the "ENV" variable and set its value to "stg"

4/ Image
Finally, we can use the `String.fromEnvironment` function to access our variable 🚀

With this we can specify:
🍭 Flavors for the web app
✍️ Misc configurations such as colors, name of the app or baseUrl
🔑 API keys to be injected in a CD step

5/ Image
So now it's your turn! 😁

Have you used --dart-define before? 🤔

What did you use it for?

I'm very curious to know! 🙌

• • •

Missing some Tweet in this thread? You can try to force a refresh

Keep Current with Gonçalo Palma

Gonçalo Palma 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!


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 @GonPalma

13 Oct
💡 Did you know that Flutter Web allows you to know the app's version?

Let's see how 👇

📱When developing for mobile, we can easily check the app's version number by going to the settings or the app store

This can be quite useful whenever a user is reporting a bug 🐛


Just to make sure our app got deployed to the stores correctly 🤦

However, Flutter Web doesn't seem to offer that out-of-the-box... or does it?

🔎 Let's explore the `build/web` folder after we build a flutter web app with

`$ flutter build web`

We have:
🖼️ An icons folder and favicons
⚙️ JS and HTML files for the Flutter app
🕸️ manifest.json for PWA


🤔 A version.json file:


Whose version and build number coincide with pubspec.yaml: 1.0.0+1

3/ Image
Read 7 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 Become our Patreon

Thank you for your support!

Follow Us on Twitter!