You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I creating and app with bloc and cubit and have several classes for each state, all extended from an master class:
@immutable
class TransactionsListState {
const TransactionsListState();
}
@immutable
class InitTransactionsListState extends TransactionsListState {
const InitTransactionsListState();
}
@immutable
class LoadingTransactionsListState extends TransactionsListState {
const LoadingTransactionsListState();
}
@immutable
class LoadedTransactionsListState extends TransactionsListState {
final List<Transaction> transactions;
const LoadedTransactionsListState(this.transactions);
}
@immutable
class FatalErrorTransactionsListState extends TransactionsListState {
const FatalErrorTransactionsListState();
}
I was using an IF-ELSE sequence to treat then on an builder:
body: BlocBuilder<TransactionsListCubit, TransactionsListState>(
builder: (context, state) {
if (state is InitTransactionsListState ||
state is LoadingTransactionsListState) {
return const Progress();
}
if (state is LoadedTransactionsListState) {
final List<Transaction> transactions = state.transactions; /* Point (1) */
but wanted to replace it with switch to make the code more clear:
body: BlocBuilder<TransactionsListCubit, TransactionsListState>(
builder: (context, state) {
switch (state.runtimeType) {
case InitTransactionsListState:
case LoadingTransactionsListState:
return const Progress();
case LoadedTransactionsListState:
final List<Transaction> transactions = state.transactions; /* Point (2) */
The problem is, when I use IF the code analysis identify the state inside the IF in the Point (1) as belonging to the LoadedTransactionsListState class.
When I use the switch this does not occurs and it throws an error:
The getter 'transactions' isn't defined for the class 'TransactionsListState'.
I fix it with an cast:
final List<Transaction> transactions =
(state as LoadedTransactionsListState).transactions;
I put an print(state.runtimeType) before the final List line and with IF or switch:
if (state is LoadedTransactionsListState) {
print(state.runtimeType);
final List<Transaction> transactions = state.transactions;
case LoadedTransactionsListState:
print(state.runtimeType);
final List<Transaction> transactions = state.transactions;
You are trying to use runtimeType, and it doesn't work (for much of anything).
In particular, you are checking whether the value of state.runtimeType (a getter which can be overridden to return anything) equals the Type object of a specific type. Even if that is true, it's not enough information to promote state to that type. Dart only promotes on == checks (for nullability) and is/as checks/casts.
I recommend actually using if statements. Not only does it promotes the variable to the tested type on a success, it also works with subtyping (if you ever wanted that).
A switch with more complicated checks, including type-checks, would be pattern matching, #1747, which the language hasn't added yet.
As a general rule, using .runtimeType is not the best solution to any real problem.
@lrhn Thanks for the information. I suspected that the type of check I suggested was complicated.
I usually prefer switch over if since I think it is more cleanner but I think there are situations where if is preferable (like the one above)
Thank you for the insign. I will close this request
I creating and app with bloc and cubit and have several classes for each state, all extended from an master class:
I was using an IF-ELSE sequence to treat then on an builder:
but wanted to replace it with
switch
to make the code more clear:The problem is, when I use IF the code analysis identify the state inside the IF in the Point (1) as belonging to the LoadedTransactionsListState class.
When I use the switch this does not occurs and it throws an error:
I fix it with an cast:
I put an print(state.runtimeType) before the final List line and with IF or switch:
Both return the same (correct) type:
Expected results:
I expected the code analysis to identify the correct class inside the
case
statement like it identifies usingif
Maybe the compiler can identify that in that particular case statement the class can belong to the extented class and not to the master class.
The text was updated successfully, but these errors were encountered: