如何在扑朔迷离的屏幕上刷新屏幕时如何调用API
Stateful:
void initState() {
_refresh();
super.initState();
}
@override
Widget build(BuildContext context) {
BlocProvider(
create: (context) => UploadedFbuBloc(repository: repository)
..add(UploadedFinalBatchUnloading()),
//lazy: false,
child: BlocBuilder<UploadedFbuBloc, UploadedFbuState>(
builder: (context, state) {
// UploadedFbuBloc(repository: repository)
// ..add(UploadedFinalBatchUnloading());
isResume = false;
if (state is UploadedFinalBatchUnloadingLoaded) {
if (state.entity.finalBatchData.isNotEmpty) {
check = true;
return Expanded(
child: RefreshIndicator(
onRefresh: _refresh,
child: ListView(
shrinkWrap: true,
children: state.entity.finalBatchData.map((item) {
return finalBatchListCard(item);
}).toList(),
),
));
} else {
check = false;
Center(child: Text('No data available'));
//
// ScaffoldMessenger.of(context).showSnackBar(
// SnackBar(content: Text("No data available")));
}
if (state is UploadedFinalBatchUnloadingError) {
check = false;
Center(
child: Text('API ERROR'),
);
}
}
return check ? CircularProgressIndicator() : Container();
// }
},
),
)
//
Future _refresh() async {
return await BlocProvider.of<UploadedFbuBloc>(context)
..add(UploadedFinalBatchUnloading());
}
Bloc:
class UploadedFbuBloc extends Bloc<UploadedFbuEvent, UploadedFbuState> {
final ApiRepository repository;
UploadedFBUEntity? entity;
var res;
UploadedFbuBloc({required this.repository})
: super(UploadedFinalBatchUnloadingEmpty()) {
on<UploadedFbuEvent>((event, emit) async {
try {
res = await repository.fetchUploadedFinalBatchUnloading();
entity = res;
print(res.toString());
print(entity);
emit(UploadedFinalBatchUnloadingLoaded(entity: entity!));
} catch (e) {
print(e);
emit(UploadedFinalBatchUnloadingError(error: e));
}
Error:
Unhandled Exception: BlocProvider.of() called with a context that does not contain a UploadedFbuBloc.
E/flutter ( 7920): No ancestor could be found starting from the context that was passed to BlocProvider.of<UploadedFbuBloc>().
E/flutter ( 7920):
E/flutter ( 7920): This can happen if the context you used comes from a widget above the BlocProvider.
E/flutter ( 7920):
E/flutter ( 7920): The context used was: FinalBatchListPage(dependencies: [MediaQuery], state: _FinalBatchListPageState#922cb)
E/flutter ( 7920):
E/flutter ( 7920): #0 BlocProvider.of (package:flutter_bloc/src/bloc_provider.dart:103:7)
E/flutter ( 7920): #1 _FinalBatchListPageState._refresh (package:cots_apron/ui/process/final_batch/FinalBatchListPage.dart:820:31)
当使用BLOC加载刷新指示器时,如何调用API? 如何使用blocprovider.of并查看我的错误,它显示了某些上下文错误。 .............................谢谢................... ................................................................................. ................................................................................. ....................................................................
Stateful:
void initState() {
_refresh();
super.initState();
}
@override
Widget build(BuildContext context) {
BlocProvider(
create: (context) => UploadedFbuBloc(repository: repository)
..add(UploadedFinalBatchUnloading()),
//lazy: false,
child: BlocBuilder<UploadedFbuBloc, UploadedFbuState>(
builder: (context, state) {
// UploadedFbuBloc(repository: repository)
// ..add(UploadedFinalBatchUnloading());
isResume = false;
if (state is UploadedFinalBatchUnloadingLoaded) {
if (state.entity.finalBatchData.isNotEmpty) {
check = true;
return Expanded(
child: RefreshIndicator(
onRefresh: _refresh,
child: ListView(
shrinkWrap: true,
children: state.entity.finalBatchData.map((item) {
return finalBatchListCard(item);
}).toList(),
),
));
} else {
check = false;
Center(child: Text('No data available'));
//
// ScaffoldMessenger.of(context).showSnackBar(
// SnackBar(content: Text("No data available")));
}
if (state is UploadedFinalBatchUnloadingError) {
check = false;
Center(
child: Text('API ERROR'),
);
}
}
return check ? CircularProgressIndicator() : Container();
// }
},
),
)
//
Future _refresh() async {
return await BlocProvider.of<UploadedFbuBloc>(context)
..add(UploadedFinalBatchUnloading());
}
Bloc:
class UploadedFbuBloc extends Bloc<UploadedFbuEvent, UploadedFbuState> {
final ApiRepository repository;
UploadedFBUEntity? entity;
var res;
UploadedFbuBloc({required this.repository})
: super(UploadedFinalBatchUnloadingEmpty()) {
on<UploadedFbuEvent>((event, emit) async {
try {
res = await repository.fetchUploadedFinalBatchUnloading();
entity = res;
print(res.toString());
print(entity);
emit(UploadedFinalBatchUnloadingLoaded(entity: entity!));
} catch (e) {
print(e);
emit(UploadedFinalBatchUnloadingError(error: e));
}
Error:
Unhandled Exception: BlocProvider.of() called with a context that does not contain a UploadedFbuBloc.
E/flutter ( 7920): No ancestor could be found starting from the context that was passed to BlocProvider.of<UploadedFbuBloc>().
E/flutter ( 7920):
E/flutter ( 7920): This can happen if the context you used comes from a widget above the BlocProvider.
E/flutter ( 7920):
E/flutter ( 7920): The context used was: FinalBatchListPage(dependencies: [MediaQuery], state: _FinalBatchListPageState#922cb)
E/flutter ( 7920):
E/flutter ( 7920): #0 BlocProvider.of (package:flutter_bloc/src/bloc_provider.dart:103:7)
E/flutter ( 7920): #1 _FinalBatchListPageState._refresh (package:cots_apron/ui/process/final_batch/FinalBatchListPage.dart:820:31)
How to call API when refresh indicator is loading by using bloc?
How to use BlocProvider.of and see my errors it is showing some context error.
.............................Thank you...........................................................................................................................................................
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将Initstate()代码更改为:
you can change initState() Code to this: