tapizquent
tapizquent

Reputation: 909

Get --dart-define environment variables in native iOS and Android

I am trying to find a way to access the environment variables provided by --dart-define inside native iOS and Android code.

Is there any way to do this?

I have tried the guide explained in https://binary-studio.com/2020/06/23/flutter-3/ but that doesn't work as what gets written to the generated .xcconfig is not separated by = but instead by %3D. And I get the error

error: .../ios/Flutter/DEFINEEnvironment.xcconfig:2: expected a ‘=’, but found % (in target 'Runner' from project 'Runner')

The file DEFINEEnvironment.xcconfig gets generated with the following content MY_VAR%3DMY_VALUE instead of MY_VAR=MY_VALUE

UPDATE

This seems to be happening because when reading from the args passed to --dart-define it must be encoding = into %3D. And I don't know how prevent that from happening

Upvotes: 2

Views: 1809

Answers (1)

tapizquent
tapizquent

Reputation: 909

It seems that the script has been changed and now includes the necessary code to decode the strings.

The post-action script that needs to be added to Scheme is:

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[@]}" > ${SRCROOT}/Flutter/DEFINEEnvironment.xcconfig

Upvotes: 1

Related Questions