Yes, you can add Dart define variables to your Flutter plugin through Gradle scripts.
In build.gradle
file of your Flutter plugin module Add the below code to the android section of the file:
android {
defaultConfig {
...
buildConfigField "String", "MY_DART_DEFINE_VAR",
"\"my_dart_define_value\""
...
}
}
In your Flutter plugin Dart code, you can access the value of the build config field,
import 'package:flutter/services.dart' show rootBundle;
const String myDartDefineVar = String.fromEnvironment('MY_DART_DEFINE_VAR',
defaultValue: 'default_value');
// Example usage
Future<String> getAssetContent(String assetPath) async {
return await rootBundle.loadString(assetPath);
}
The String.fromEnvironment
method will retrieve the value of the MY_DART_DEFINE_VAR
build config field and assign it to the myDartDefineVar
constant. If the build config field is not defined, it will use the default value default_value
.
You can use the myDartDefineVar
constant in your Flutter plugin code as needed.
Remember that you will need to rebuild your Flutter plugin module and update the AAR
file in your main Android app to use the updated plugin with the new Dart define variables.