Dynamic Values when buiding Flutter app

Let me explain the usecase

I’m working on a flutter app that talks to REST API. During the development, the server wil lbe on localhost but in production it would be real server.

Solution ? Use --dart-define

Way to do this is pass the value during the build time like flutter build apk --dart-define=BASE_URL=http://10.0.2.2:8000

10.0.2.2 is a special alias to your host machine from within the Android emulator. 1 When running the app on an Android emulator, it allows access to your local development server.

During the production build, use something like

flutter build apk --dart-define=API_URL=https://api.myserver.com

There is also more complex option called flavors - but that is for another post 😄 For now --dart-define is enough.


  1. Apparenly this won’t work when testing on physical Android device connected (via USB) to the development machine unless your server is accessible from the phone’s network and the IP is correct. (Maybe use something like ngrok 🤔) ↩︎