Dynamically change base url of your dio client or http client at runtime.
📚 Guide
- Initialize
dynamic_base_url
usingBASEURL.init
insidemain()
method.
void main() {
WidgetsFlutterBinding.ensureInitialized();
/// Initialize BASE URL
BASEURL.init(
debug: 'https://dummyjson.com',
prod: 'https://jsonplaceholder.typicode.com',
);
runApp(const MyApp());
}
- Add
BaseWrapper
inMaterialApp.builder
.
.....
@override
Widget build(BuildContext context) {
return MaterialApp(
builder: (context, child) {
/// Set up [BaseWrapper]
return BaseWrapper(
builder: (context) => child!,
onBaseUrlChanged: () {},
);
},
home: const BaseURLUseExample(),
);
}
.....
- 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 fromBaseWrapper
callback.
.....
return BaseWrapper(
builder: (context) => child!,
onBaseUrlChanged: () {
/// Change [baseUrl] here
GetIt.instance<Dio>().options.baseUrl = BASEURL.URL;
},
);
.....
- 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: