License: MIT

Dynamically change base url of your dio client or http client at runtime.

📚 Guide

  1. Initialize dynamic_base_url using BASEURL.init inside main() method.
void main() {
  WidgetsFlutterBinding.ensureInitialized();
  
  /// Initialize BASE URL
  BASEURL.init(
    debug: 'https://dummyjson.com',
    prod: 'https://jsonplaceholder.typicode.com',
  );

  runApp(const MyApp());
}
  1. Add BaseWrapper in MaterialApp.builder.
.....
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      builder: (context, child) {
        /// Set up [BaseWrapper]
        return BaseWrapper(
          builder: (context) => child!,
          onBaseUrlChanged: () {},
        );
      },
      home: const BaseURLUseExample(),
    );
  }
.....
  1. Use BASEURL.URL in dio or http.
  • Direct
_dio.get("${BASEURL.URL}/todos/1")
  • Dio
/// User [BASEURL.URL] as a base url
_dio.options.baseUrl = BASEURL.URL;
  • If you are using service locator get_it then change client base url from BaseWrapper callback.
.....
return BaseWrapper(
  builder: (context) => child!,
  onBaseUrlChanged: () {
    /// Change [baseUrl] here
    GetIt.instance<Dio>().options.baseUrl = BASEURL.URL;
  },
);
.....
  1. Open Base URL settings using floating bubble.

License

MIT License

Copyright (c) 2023 Ashutosh Mulik

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

Libraries

dynamic_base_url