1

I have implemented a flutter web app and created develop and release environments. I am accessing the API Url like "${ConfigEnv.baseUrl}${MyEndPointFile.jokesEndPoint}", . The Config File looks like below:

class ConfigEnv{
   static String get fileName {
    if(kReleaseMode){
      return '.env.production';
    }
    return '.env.develop';
  }

  static String get baseUrl{
    return dotenv.env['API_URL'] ?? '';
  }
}

When I run my web app locally in develop mode or release mode then the correct base urls are picked as written in .env file but when I host the web app to custom domain then the base url is getting replaced by the domain name of my hosted web app. To simplify and debug the problem, when I replace the interpolated string (baseUrl+endPoint) by complete hardcoded API URL then it is working on deployed live website as well. Why is this happening and how to resolve this?

5
  • Please provide more debugging details. For example, how is the webapp fetching the env file? Commented Jan 31, 2024 at 14:33
  • My .env file has the API base URK for develop and similar another .env for production API base URL. For example: my .env.production looks like API_URL = testproduction-api-929265sb5q-uc.a.app. Commented Feb 7, 2024 at 6:03
  • To simplify and debug the problem, when I replace the interpolated string (baseUrl+endPoint) by complete hardcoded API URL then it is working on deployed live website as well. * This hardcoded url i wrote in simple dart file where my api request is written like http.get('my_ap_url') in controller. Commented Feb 7, 2024 at 6:05
  • Have you tried printing the value to the browser console in your 'production' mode to see if it's right or wrong? In production, where is the "env" file stored? Commented Feb 7, 2024 at 11:34
  • Yes I tried printing the env variable on UI in localhost by running command flutter run --release and it is printing correct value on my UI. // Text("Loaded .env file: ${ConfigEnvironment.baseUrl}"), and my .env files are in the root (the same hierarchy in which the android folder/ios folder/ lib folder resides) Commented Feb 8, 2024 at 10:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.