Flutter 2.2, Dart Defines and breaking changes. Here we go again…

Denis Beketsky
3 min readMay 19, 2021

--

This article is an addition to the main article about compile-time variables. If you don’t know how to define and use them, I would recommend you to read the main article before you continue:

Hello everyone. Flutter team just recently released a new stable version. This means it’s time to talk about breaking changes 😉

Image is taken from https://memegenerator.net/

So if you’re using Dart Defines and you not receiving these variables in your native code after upgrade to Flutter 2.2, this article is for you)

What was changed?

Basically, it’s the second attempt from the Flutter team to fix the Dart Define definition and its usage. The first attempt was in the 1.20 release when they decided to use URL encode. But it seems that it didn’t solve all problems.

According to GitHub pull request — the issue was related to building Flutter on Windows. Where % was considered as a control character. So this time they decided to replace URL encode with Base64 encode.

Decoding for Android

In order to fix the Android build, you need to decode each Dart Define item before it assigned to the result HashMap. So in the original GitHub project, you just need to change 1 line in the Gradle config and add Base64 decode before you split dart define item on key and value pair (line 5 in the following code snippet):

That’s all.

Decoding for iOS

In order to parse dart-define value in Xcode, you need to update the pre-action script.

Open your flutter project in XCode and Edit Scheme.

Select Build > Pre-action section:

And update your script with the following variant.

You can read a detailed explanation in this article:

But if you already using Dart Defines in an iOS project, you probably familiar with it. The difference compared to the version from Flutter 1.20 is in line number 3. Where instead of URL decode it’s now doing Base64 decode. I also renamed this function to keep its name consistent with what it does 🤓

And that’s all.

Now your project is ready to work with a Flutter 2.2 and higher.

Example project with all changes is placed as usual here, use master branch for Flutter version 2.2 or flutter-1.19 branch for Flutter before 2.2:

Thank you for your time and happy coding!

--

--