Predefined configurations
When you have multiple mix calls that share the same configuration, you can use
the config parameter with MixConfig to avoid repetition and ensure consistency.
Basic usage
Section titled “Basic usage”Create a reusable configuration:
// Define onceconst checkInternetRetryAndLog = MixConfig( retry: retry(maxRetries: 5, initialDelay: 1.sec), checkInternet: checkInternet, before: () => Log.info("Starting"), after: () => Log.info("Finished"), catchError: (error, stackTrace) => Log.error("Failed", error),);
// Use everywherevoid fetchUsers() { mix( key: this, config: checkInternetRetryAndLog, // Here! () async { final users = await api.getUsers(); emit(state.copyWith(users: users)); }, );}Available configuration options
Section titled “Available configuration options”MixConfig supports all the same parameters as mix:
const myConfig = MixConfig( // Feature configurations retry: retry(...), checkInternet: checkInternet(...), nonReentrant: nonReentrant(...), throttle: throttle(...), debounce: debounce(...), fresh: fresh(...), sequential: sequential(...),
// Callbacks before: () { ... }, // Called once before the first attempt after: () { ... }, // Called once after all attempts complete wrapRun: (action) { ... }, // Wraps each execution of the action catchError: (error, stack) { ... }, // Handle errors);Resolution order
Section titled “Resolution order”Configuration values are resolved in this order (highest priority last):
- Built-in defaults (e.g.,
RetryConfig.defaults) - Config parameter (
config: myConfig) - Explicit parameters (e.g.,
retry: retry(...))
This means you can define base configurations and override specific values per-call:
const serverCallConfig = MixConfig( retry: retry(maxRetries: 5, multiplier: 3.0),);
// Uses config's maxRetries=5 and multiplier=3.0void normalCall() { mix( key: this, config: serverCallConfig, () async { ... }, );}
// Overrides maxRetries to 10, but keeps multiplier=3.0 from configvoid importantCall() { mix( key: this, config: serverCallConfig, retry: retry(maxRetries: 10), () async { ... }, );}Customizing defaults globally
Section titled “Customizing defaults globally”Each config class has a static defaults property you can customize at app startup:
void main() { // Customize defaults for your app RetryConfig.defaults = retry( maxRetries: 5, initialDelay: Duration(milliseconds: 200), multiplier: 2.0, maxDelay: Duration(seconds: 10), );
ThrottleConfig.defaults = throttle( duration: Duration(seconds: 1), removeLockOnError: false, ignoreThrottle: false, );
runApp(MyApp());}Now any MixConfig or explicit parameter that doesn’t specify a value will use your
custom defaults instead of the built-in ones.