我可以通过Flutter Bloc中的“ yield”运算符来管理进行中状态,
我的集团:
@override
Stream<ContentState> mapEventToState(
ContentEvent event,
) async* {
if (event is ContentStarted) {
yield ContentLoadInProgress(); //yeah
var content= await repository.getContent();
yield ContentLoadSuccess(content);
}
...
}
页面:
builder: (context, state) {
if (state is ContentInProgress) {
return LoadingWidget(); //showing CircularProgressIndicator Widget
} else if (state is ContentLoadSuccess) {
return Text(state.content);
}
(状态:InitState,ContentLoadInProgress,ContentLoadSuccess,ContentLoadFailure)
如何在Provider State Management中管理“ ContentLoadInProgress”状态?
答案 0 :(得分:1)
您可以将状态保持为enum
enum ContentStates {
InitState,
ContentLoadInProgress,
ContentLoadSuccess,
ContentLoadFailure,
}
在您的提供者类中:
class ContentProvider with ChangeNotifier {
ContentState state = ContentStates.InitState;
Content content;
yourEvent() {
state = ContentStates.ContentLoadInProgress;
notifyListeners(); // This will notify your listeners to update ui
yourOperations();
updateYourContent();
state = ContentStates.ContentLoadSuccess;
notifyListeners();
}
}
您可以在小部件内部使用Consumer
(假设您已经在小部件树中使用了上面的ChangeNotifierProvider
)
Consumer(
builder: (context, ContentProvider provider, _) {
if (provider.state == ContentStates.ContentLoadInProgress) {
return LoadingWidget();
} else if (provider.state == ContentStates.ContentLoadSucces) {
// use provider.content to get your content
return correspondingWidget();
} else if .... // widgets for other states
}
)