大家好,希望你们一切都好!
我有一个问题,如何在课堂上使用 switch case? (这里的'state'是主类,其他类都是从state继承的。)
例如,我尝试替换这个:
if(state is LoadingPage) {return LoadingPage;}
else if {state is SuccessPage) {return SuccessPage;}
else if {state is FailPage) {return FailPage;}
对此:
switch (state){
case LoadingPage: {return LoadingPage;}
case SuccessPage: {return SuccessPage;}
case FailPage: {return FailPage;}
}
它不起作用,对我有用的是将状态设置为字符串,如下所示:
switch (state.toString()){
case 'LoadingPage': {return LoadingPage;}
case 'SuccessPage': {return SuccessPage;}
case 'FailPage': {return FailPage;}
}
它虽然有效,但这是一个很短的时间解决方案,就像如果我必须编辑某些内容,那么很难更改所有内容(因为它是一个字符串)..
有一个名为
freezed
的包用于此精确解决方案,其中我们不使用 switch
而是使用 map
和/或 maybeMap
。
@freezed
class MemberState with _$MemberState {
factory MemberState.initial() = _MemberStateInitial;
factory MemberState.loading() = _MemberStateLoading;
factory MemberState.loadSuccess({required MemberDetail detail}) =
_MemberStateLoadSuccess;
factory MemberState.loadFailure({required AppFailure failure}) =
_MemberStateLoadFailure;
}
用途:
@override
Widget build(BuildContext context) {
return BlocBuilder<MemberBloc, MemberState>(
builder: (context, state) {
return const Scaffold(
body: state.map(
initial: initial,
loading: loading,
loadSuccess: loadSuccess,
loadFailure: loadFailure),
);
},
);
}
当然,我将它与 BLoC 一起使用,但基本概念是相同的。您可以以任何您喜欢的方式使用它。而且,最好的事情是它都是自动生成的。因此,您不必自己编写代码。
这样做:
switch (state.runtimeType){
case LoadingPage: {return LoadingPage;}
case SuccessPage: {return SuccessPage;}
case FailPage: {return FailPage;}
}
您正在寻找 switch 表达式。 Dart 团队刚刚在 Dart V3 中添加了此功能。
enum PageState { LoadingPage, SuccessPage, FailPage }
void main() {
final state = PageState.SuccessPage;
final text = switch (state) {
PageState.LoadingPage => 'LoadingPage',
PageState.SuccessPage => 'SuccessPage',
PageState.FailPage => 'FailPage',
_ => 'None'
};
print(text);
}
欲了解更多详情,您可以阅读这里
switch
使用 Dart 3.0 模式匹配class Dog {
Dog({required this.name});
final String name;
}
class Cat {
Cat({required this.name});
final String name;
}
String checkType(dynamic v) {
String type;
switch (v) {
case Dog(:final name):
type = 'dog, $name';
case Cat cat:
type = 'cat, ${cat.name}';
case int:
type = 'int';
case (String name, String age):
type = 'Record ($name, $age)';
case {'user': {'name': final String name, 'age': final int age}}:
type = 'User Json $name, $age';
default:
type = 'unknown';
}
return type;
}
switch (v) {
case Dog(name:final name):
// or You can skip name if use same name.
switch (v) {
case Dog(:final name):
⚠️文档:如果值不具有相同的类型,它们就会被驳斥。
您应该使用正确的类型来破坏对象。
只需使用
final
或 var
来指示数据类型,以便您始终使用相同类型的对象字段。
return switch (v) {
Dog(:String age) => 'dog, $age', // Compile error 'The getter 'age' isn't defined for the type 'Dog'.'
Dog(name: String otherName) => 'dog, $otherName', // OK, Success to match
Dog(:int name) => 'dog, $name', // Fail to match
Dog(:String name) => 'dog, $name', // Success to match, But can't to be reached.
final
的最终变量)case Cat cat:
type = 'cat, ${cat.name}';
or
case final Cat cat:
type = 'cat, ${cat.name}';
case (String name, String age):
type = 'Record ($name, $age)';
case {'user': {'name': final String name, 'age': final int age}}:
type = 'User Json $name, $age';
switch
表达。String checkType(dynamic v) {
return switch (v) {
Dog(:final name) => 'dog, $name',
Cat cat => 'cat, ${cat.name}',
int => 'int',
(String name, String age) => 'Record ($name, $age)',
{'user': {'name': final String name, 'age': final int age}} =>
'User Json $name, $age',
_ => 'unknown',
};
}
您可以在Dart 文档
中查看更多信息