'Flutter 2 issue: XCode cannot access compile-time variables ($DART_DEFINES)

I am trying to migrate a Flutter project from Flutter v1.22.4 to Flutter v2.0.6.

In the flutter run command, we define some dart-define variables like --dart-define=DEFINEEXAMPLE_APP_NAME=My_App_Staging --dart-define=DEFINEEXAMPLE_APP_SUFFIX=staging.

Then, we use a pre-build action like the one defined in the article below, to use the dart-define variables as environment variables:

https://itnext.io/flutter-1-17-no-more-flavors-no-more-ios-schemas-command-argument-that-solves-everything-8b145ed4285d

function urldecode() { : "${*//+/ }"; echo "${_//%/\\x}"; }

IFS=',' read -r -a define_items <<< "$DART_DEFINES"


for index in "${!define_items[@]}"
do
    define_items[$index]=$(urldecode "${define_items[$index]}");
done

printf "%s\n" "${define_items[@]}"|grep '^DEFINEEXAMPLE_' > ${SRCROOT}/Flutter/flutter.xcconfig

The script worked perfectly in Flutter v1.22.4, but it's broken in Flutter v2.0.6. More specifically, nothing is printed when I add echo "$DART_DEFINES" to the beginning of the script, which suggests to me that $DART_DEFINES is null when passed to the pre-action. As a result of this, my build is failing.

One thing to note is that DART_DEFINES is defined correctly inside these two files auto-generated by Flutter during the build:

  • ios/Flutter/Generated.xcconfig
  • ios/Flutter/flutter_export_environment.sh

What could be causing this issue?



Solution 1:[1]

I had the same issue and soved it like this.

  1. Most likely you did not provide anything as "Profice build settings from". The default is "None", unfortunately. You have to give your target, which is "Runner" per default. [Edit the Schema[Choose Runner]

  2. The other thing I noticed will not solve the issue that you described but must be fixed too. With version 2.x the value of DART_DEFINES is Base64 encoded. Instead of your function urldecode use somthing like:

    function entry_decode() { echo "${*}" | base64 --decode; }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1