flutter_env_native

Creator: coderz1093

Last updated:

Add to Cart

Description:

flutter env native

flutter_env_native #

A plugin/utility that provides compile-time variables for native platform.

This plugin/utility makes env variables set in --dart-define and --dart-define-from-file available to the native platforms.

🎖 Installing #
dev_dependencies:
flutter_env_native: ^0.1.0
copied to clipboard
🎮 Setup Guide #
Define .env file #
APP_NAME=batcave
APP_SUFFIX=.dev
MAPS_API_KEY=someKeyString
copied to clipboard
Pass .env file to flutter run/build via --dart-define-from-file=.env
Android Installation #
📂 android/app/build.gradle #
❗ DO NOT OMIT ANY OF THE FOLLOWING CHANGES ❗
// flutter_env_native
+Project flutter_env_native = project(':flutter_env_native')
+apply from: "${flutter_env_native.projectDir}/envConfig.gradle"
copied to clipboard
Usage in android manifest file #
📂 android/app/src/main/AndroidManifest.xml:
❗ DO NOT OMIT ANY OF THE FOLLOWING CHANGES ❗
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId = "tech.mastersam.flutter_env_example"
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
minSdk = flutter.minSdkVersion
targetSdk = flutter.targetSdkVersion
versionCode = flutterVersionCode.toInteger()
versionName = flutterVersionName

+ resValue "string", "app_name", APP_NAME ?: 'default_name'
}
copied to clipboard
The above create a string resource with variable app_name set to the value of APP_NAME
<application
android:label="flutter_env_example"
+ android:name="@string/app_name"
android:icon="@mipmap/ic_launcher">
copied to clipboard
The above sets the android:name to the value of app_name in the string resource. The same applies for other .xml files.
Usage in kotlin file #
📂 android/app/src/main/AndroidManifest.xml:
❗ DO NOT OMIT ANY OF THE FOLLOWING CHANGES ❗
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId = "tech.mastersam.flutter_env_example"
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
minSdk = flutter.minSdkVersion
targetSdk = flutter.targetSdkVersion
versionCode = flutterVersionCode.toInteger()
versionName = flutterVersionName

+ buildConfigField "String", "APP_NAME", "\"${APP_NAME ?: 'default_name'}\""
}
copied to clipboard
The above creates the BuildConfig field APP_NAME and sets its value to the env variable APP_NAME where available and defaults it to 'default_name'
📂 android/.../MainActivity.kt:
❗ DO NOT OMIT ANY OF THE FOLLOWING CHANGES ❗
package tech.mastersam.flutter_env_example

import android.os.Bundle
import android.util.Log
import android.widget.Toast
import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// Access BuildConfig values
+ val appName = BuildConfig.APP_NAME

// Sample usage to display a toast message
Toast.makeText(this, "APP_NAME: $appName", Toast.LENGTH_LONG).show()
}
}

copied to clipboard
iOS Installation #
📂 ios/Podfile #
❗ DO NOT OMIT ANY OF THE FOLLOWING CHANGES ❗
post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
end

+ flutter_env_plugin_path = File.join(Dir.pwd, '.symlinks', 'plugins', 'flutter_env_native', 'ios', 'setup_env.sh')
+ root_dir = File.expand_path('..', Dir.pwd)

+ # Run the setup_env.sh script from the plugin
+ system("sh \"#{flutter_env_plugin_path}\" \"#{root_dir}\"")
end
copied to clipboard
Usage in plist file #
📂 info.plist:
Reference the env variable directly as defined in the .env file
<key>CFBundleDisplayName</key>
<string>$(APP_NAME)</string>
copied to clipboard
Usage in swift file #
📂 AppDelegate.swift: (any swift file of choice)
+ var app_name: String = Bundle.main.infoDictionary?["APP_NAME"] as? String ?? ""
NSLog("\nHere's your app name -> \(app_name)")
copied to clipboard

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files:

Customer Reviews

There are no reviews.