Is there a way to trigger a BlocListener to listen to all changes just after its initialization?

40 Views Asked by At

I need to run the listener once the page opens and listen to all changes from loading to the main emit, i followed this question Is there a way to trigger a BlocListener just after its initialization?

I do cascade operation in the Blocprovider and it works fine with only the last state, the change in state is so fast that the value will change before the listener is connected so even this will not work unless you add an unnecessary Future.delayed which I don't need it all, so how the bloclistner listen to all changes and emits not only the last one once the page opens

here is how I call the bloc:

  case Routes.mainRoute:
        // ---- Main Screen
        return MaterialPageRoute(
          builder: (_) => MultiBlocProvider(
            providers: [
              BlocProvider(
                  create: (BuildContext context) => AddressCubit(
                        AddressRepository(addressApi: AddressApi()),
                      )..getCurrentLocation()),
            ],
            child: const OnMapScreen(),
          ),
        );

and here is my cubit

void getCurrentLocation({double? lat, double? long}) async {

    emit(GetCureentLocationLoadedState());

   bool serviceEnabled;
   LocationPermission permission;

  serviceEnabled = await Geolocator.isLocationServiceEnabled();
  if (!serviceEnabled) {
    emit(ServiceNotEnabledState(
        message: "You need to enable location from your mobile"));
  }

  permission = await Geolocator.checkPermission();

  if (permission == LocationPermission.always ||
      permission == LocationPermission.whileInUse) {
    Position position = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high);

    List<Placemark> placemarks = await placemarkFromCoordinates(
        position.latitude, position.longitude);

    emit(LocationSuccessCase());
  }

}

and here is my UI

BlocConsumer<AddressCubit, AddressState>(
              listener: (context, state) {
            if (state is GetCureentLocationLoadedState) {
             
              );
            } else if (state is ServiceNotEnabledState) {
              
            } else if (state is LocationSuccessCase) {
           
            } else {
           
            }
          }, builder: (context, state) {
            return GoogleMap(
              mapType: MapType.normal,
                               markers: markers.toSet(),
              initialCameraPosition: _openedLocation,
              onTap: (LatLng latlng) async {
                addressCubit!.getCurrentLocation(
                  lat: latlng.latitude,
                  long: latlng.longitude,
                );
                setState(() {});
              },
              onMapCreated: (mapConroller) {
                context.read<AddressCubit>().getCurrentLocation();
                gmc = mapConroller;
              },
            );
          }),
        ),

the bloclistner listen to only the last emit which is LocationSuccessCase, not GetCureentLocationLoadedState firstly

0

There are 0 best solutions below