admin管理员组文章数量:1277398
I'm trying to combine Material Autocomplete widget with Bloc pattern for Google Place Autocomplete function.
My code as below:
BlocBuilder<MyBloc, MyState>(
builder: (context, state) {
final bloc = context.read<MyBloc>();
return Autocomplete<GooglePlace>(
optionsBuilder: (textEditingValue) {
bloc.add(MakeAutocompleteEvent(textEditingValue));
return state.places;// << this line does not return the value of current state when I emit the state after making autocompletes request
}
...
Autocomplete class
With this approach, in optionsBuilder
, the widget call for text changed and return the Iterable of result in the same function.
We can await and callling API for result, then return the result here.
But this won't work for bloc pattern.
Tried to set value to textEditingController
after making the request and the state has the data, the widget rebuilt but the optionsBuilder
was not called.
I expect to make Autocomplete work with Bloc pattern.
Is there any approach for this combination? Thank you.
I'm trying to combine Material Autocomplete widget with Bloc pattern for Google Place Autocomplete function.
My code as below:
BlocBuilder<MyBloc, MyState>(
builder: (context, state) {
final bloc = context.read<MyBloc>();
return Autocomplete<GooglePlace>(
optionsBuilder: (textEditingValue) {
bloc.add(MakeAutocompleteEvent(textEditingValue));
return state.places;// << this line does not return the value of current state when I emit the state after making autocompletes request
}
...
Autocomplete class
With this approach, in optionsBuilder
, the widget call for text changed and return the Iterable of result in the same function.
We can await and callling API for result, then return the result here.
But this won't work for bloc pattern.
Tried to set value to textEditingController
after making the request and the state has the data, the widget rebuilt but the optionsBuilder
was not called.
I expect to make Autocomplete work with Bloc pattern.
Is there any approach for this combination? Thank you.
Share Improve this question edited Feb 24 at 7:52 Trung asked Feb 24 at 6:06 TrungTrung 113 bronze badges1 Answer
Reset to default 0This is exactly what I'm looking for: "Waiting for state change"
// Add event to bloc
bloc.add(MakeAutocompleteEvent(textEditingValue.text));
// Wait for successful or failed state to be emitted
// Change the condition for the state you want to get here
final newState = await bloc.stream.firstWhere((state) =>
state.status is SubmissionSuccess || state.status is SubmissionFailed);
// Check and return
if (newState.status is SubmissionSuccess) {
return newState.places;
} else {
return const Iterable.empty();
}
本文标签: flutterHow to make Material Autocomplete work with BlocStack Overflow
版权声明:本文标题:flutter - How to make Material Autocomplete work with Bloc? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741290872a2370540.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论