skip to Main Content

I am developing a flutter app with backend running in localhost.

I am using http package for making http calls. I am creating the Uri like this:

Uri.https("localhost:8080", "api/v1/login");

but this Uri.https gives error, instead I need to use Uri.http.

The problem is when the app is ready for production I will have to manually edit code everywhere to use Uri.https instead of Uri.http.

Is there a better way to handle this?

2

Answers


  1. You can simple use kReleaseMode to detect you are in release mode or not.

    String baseUrl;
    
    if (kReleaseMode) {
      baseUrl = 'https://example.com';
    } else {
      baseUrl = 'http://localhost';
    }
    
    Uri uri = Uri.parse('$baseUrl/api/v1/login');
    
    Login or Signup to reply.
  2. you can do it like this:

     var baseUrl = 'http://localhost:8080/';
    
      Future<void> _fetchData() async {
        final response = await http.get(Uri.parse('$baseUrl/api/v1/login'));
        print(response.body);
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search