4

There is a problem with the first call I have to get the value list of values from the first API to make a second API call. how do update the data to UI. I have two listviews one is for loading from the first API and the second listview is for the second API

After getting a response from the first API listview2 gets a null value getting an exception. because the state is not emitted of FacilityUnitsload

My code is below. What am I missing?

 _loadstoragetype(){_bookingCubit.facilitystoragetype());}
 _loadfacilityunits(){   _bookingCubit.facilitystorageunits(jsonEncode(
  StoragetypeunitsRequest(storagetypelist,locationlist))); }

@override
void didChangeDependencies() {      
   _loadstoragetype();      
   super.didChangeDependencies();
   }
@override
Widget build(BuildContext context) {
return
  MultiBlocProvider(
  providers: [
    BlocProvider<BookingCubit>.value(value: _bookingCubit),
  
  ],
  child: Scaffold(        
    body:  BlocProvider.value(
  value: _bookingCubit,
  child: BlocConsumer<BookingCubit,BookingState>(
    builder: (context, state) {

      if (state is BookingLoading) {
        return Loading();
      } else if (state is ErrorState) {
        return ErrorTxt(
          message: '${state.error}',
          ontap: () => _loadtype(),
        );
      }

      if (state is FacilityUnitTypeload) {
        if (state.storageTypeModel.isSuccess!) {            
          
          _loadfacilityunits();
          return ListView.builder(
                        itemCount: storagetypelist?.length,
                        scrollDirection: Axis.horizontal,
                        itemBuilder: (BuildContext context, int index) {
                          return GestureDetector(
                            onTap: () => '',
                            child:Text('${storagetypelist[index].name}'),
                          );
                        },)
        } else {
          return Text('Error')
        }
      }

      if (state is FacilityUnitsload) {
       
        if (state.storageTypeUnits.isSuccess!) {
                         
          return Expanded(
        child: ListView.builder(
          physics: NeverScrollableScrollPhysics(),
          itemCount: stypeunits.length,
          itemBuilder: (BuildContext context, int index) {
         
            return Text('${state.storageTypeUnits.result[index].name}'),
          },
        ));
        } else {
          return Text('Error')

        }
      }
      return //_buildbookunits();
    },
    listener: (context, state) {

    },
    buildWhen: (context, state) {
      return state is FacilityUnitTypeload;
    },
  ),
)
);}
kumar
  • 111
  • 12
  • You can ```await``` the second api call unless the first one is finished. Wrap the whole api request in an ```async``` function. For eg- ```Future myApiRequest() async{ await firstRequest(); await secondRequest(); }``` – Arijeet Apr 02 '22 at 05:19
  • hi could u please help this https://stackoverflow.com/questions/71723270/flutter-bloc-cubit-listener-progress-bar-not-showing-and-also-data-not-changed-i – kumar Apr 04 '22 at 07:44

0 Answers0