P_equals_NP_2021
P_equals_NP_2021

Reputation: 707

Flutter How To Ensure a Function is Called Whenever App Hot Reloads in Android Studio?

I have a Flutter App that relies on a correct Server URL being set based on the environment it is running in. However, my function seems to not be called during Hot reloads.

I have at the top of my main.dart:

String TARGET_URL; /// TODO: Replace once figure out how to set environment Variables based on type of device.

void setTargetURLForEnv() async {
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
  if (Platform.isIOS) {
    IosDeviceInfo iosDeviceInfo = await deviceInfo.iosInfo;
    if (iosDeviceInfo.isPhysicalDevice) {
      TARGET_URL = 'http://aws-server-url.com';
    } else {
      TARGET_URL = 'http://localhost:6900';
    }
  }
}

void main() async {
  runApp(App());
  setTargetURLForEnv();
}

Hot Reloads cause TARGET_URL to be null, even when running on a iOS simulator. How do I force this method to be called?

Upvotes: 0

Views: 482

Answers (1)

Anandha Krishnan Aji
Anandha Krishnan Aji

Reputation: 162

Try setTargetURLForEnv() above the runApp(App()). I think because it is outside the App(), hot reloading the app won't execute the function. Try hot restart ('R') instead of hot reload in terminal.

Upvotes: 1

Related Questions