'how to call api while refreshing the screen with bloc in flutter

        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...........................................................................................................................................................



Solution 1:[1]

you can change initState() Code to this:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ?????