BlocProvider 内部没有返回值
Either inside BlocProvider is not returning value
我想使用 Either
将结果存储在 BlocBuilder
中。根据结果我想显示具体 Widget
。但是使用 Either
会抛出 BlocBuilder
returned null
的异常。我调试了那个代码, fold
分支被正确调用,所以它应该 return Widget
。我不明白它怎么会 return null
.
代码:
...
else if (state is Loaded) {
final badgeOrFailure = state.profile.getBadgeByOrgId(orgId);
badgeOrFailure.fold((err) {
return MessageDisplay(
message: err.message,
);
}, (badge) {
return BadgeWidget(
desc: badge.desc,
code: badge.code,
);
});
...
个人资料代码:
Either<BadgeNotFoundFailure, Badge> getBadgeByOrgId(int orgId) {
try {
if (badges != null && badges.isNotEmpty) {
return Right(badges.firstWhere((element) => element.orgId == orgId));
} else {
log('badges are empty');
return Left(BadgeNotFoundFailure());
}
} on Exception catch (_) {
return Left(BadgeNotFoundFailure());
}
}
我认为你需要一个明确的return
,例如
return badgeOrFailure.fold(...
你能试试吗?
我想使用 Either
将结果存储在 BlocBuilder
中。根据结果我想显示具体 Widget
。但是使用 Either
会抛出 BlocBuilder
returned null
的异常。我调试了那个代码, fold
分支被正确调用,所以它应该 return Widget
。我不明白它怎么会 return null
.
代码:
...
else if (state is Loaded) {
final badgeOrFailure = state.profile.getBadgeByOrgId(orgId);
badgeOrFailure.fold((err) {
return MessageDisplay(
message: err.message,
);
}, (badge) {
return BadgeWidget(
desc: badge.desc,
code: badge.code,
);
});
...
个人资料代码:
Either<BadgeNotFoundFailure, Badge> getBadgeByOrgId(int orgId) {
try {
if (badges != null && badges.isNotEmpty) {
return Right(badges.firstWhere((element) => element.orgId == orgId));
} else {
log('badges are empty');
return Left(BadgeNotFoundFailure());
}
} on Exception catch (_) {
return Left(BadgeNotFoundFailure());
}
}
我认为你需要一个明确的return
,例如
return badgeOrFailure.fold(...
你能试试吗?