I have created these unions using Freezed:
abstract class ProductState with _$ProductState {
factory ProductState.loading() = ProductLoading;
factory ProductState.created(Product product) = ProductCreated;
}
Then I have a provider providing a class like this:
final createProductProvider =
StateNotifierProvider<CreateProductController, ProductState>(
(ref) => CreateProductController(ref));
class CreateProductController extends StateNotifier<ProductState> {
createProduct() {
// create product
state = ProductCreated(product);
}
}
In another provider I want to access the underlying data in the union, the product variable.
I have been doing it like this so far:
if(state is ProductCreated) {
final currentProduct = state.product
}
What is the proper way to do this? Create another provider?