如何在扑朔迷离的屏幕上刷新屏幕时如何调用API

发布于 2025-01-24 07:03:32 字数 4332 浏览 0 评论 0原文

        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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜空下最亮的亮点 2025-01-31 07:03:32

您可以将Initstate()代码更改为:

void initState() {
  super.initState();
  Future.delayed(Duration(seconds: 0), () {
    _refresh();
  });
}
  

you can change initState() Code to this:

void initState() {
  super.initState();
  Future.delayed(Duration(seconds: 0), () {
    _refresh();
  });
}
  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文