Skip to content

Wrap with mix

Consider a Cubit with one or more methods:

class UserCubit extends Cubit<User> {
UserCubit() : super(User());
void loadData() {
// Load something
}
void saveData() {
// Save something
}
}

We start by wrapping our Cubit methods with the mix function provided by the Bloc Superpowers package. The only required parameters are a unique key and the action callback:

class UserCubit extends Cubit<User> {
UserCubit() : super(User());
void loadData() => mix(
key: this,
() async {
// Load something
}
);
void saveData() => mix(
key: this,
() async {
// Save something
}
);
}

 

These are all valid ways to use the mix function, depending on what you want to return from the action callback:

// Arrow function syntax
void doSomething() => mix(
key: this,
() async { ... }
);
// Arrow function syntax returning a Future<void>
Future<void> doSomething() => mix(
key: this,
() async { ... }
);
// Arrow function syntax returning a Future<int>
Future<void> doSomething() => mix(
key: this,
() async {
...
return 42;
}
);
// Block function syntax
void doSomething() {
mix(
key: this,
() async { ... }
);
);
// Block function syntax returning a Future
Future<void> doSomething() async {
await mix(
key: this,
() async { ... }
);
);

The mix function also accepts several optional parameters that add powerful features to your Cubit methods. Here are all of them used together, with their default values:

void doSomething() {
mix(
key: this,
retry: retry,
nonReentrant: nonReentrant,
checkInternet: checkInternet,
fresh: fresh,
debounce: debounce,
throttle: throttle,
sequential: sequential,
config: config,
() async {
// Do something
}
);
}

In the next pages, let’s see how each of these parameters work in detail.