Is it possible to get information from a terminal into a flutter app? If yes, please share an example

Issue

I’m writing a flutter app that needs to set up elasticsearch on first run. To do this, I need to run some commands in the terminal, I know how to do it, but I also need information from the terminal for a complete setup. How to get information from terminal to my flutter app, can i assign the value of one of the terminal strings to some variable in flutter?

I not found a some issues about getting info from terminal to app.

Solution

You can use --dart-define from the terminal while running your app or while building your application.

for example

flutter run --dart-define=BASE_URL=https://flutter.dev

Here BASE_URL is a key and you can pass value to it, you can name it as per your need.

And, You can access this in your Flutter app like below:

const String.fromEnvironment('BASE_URL')

You can check out this video as well for the same: Passing values from the command line to Flutter app

const is required while accessing that value you pass using dart-define and the key name is case sensitive.

Answered By – ibhavikmakwana

Answer Checked By – Marilyn (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *