Home > OS >  Use http urls with Flutter http during development
Use http urls with Flutter http during development

Time:12-31

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?

CodePudding user response:

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');

CodePudding user response:

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);
  }
  • Related