Max
Max

Reputation: 1311

How to refresh a page after deleting data without revisiting?

I have a page with data from a server. I receive the data through BloC. I also have a button with which I delete the data. When I delete the data should stateElectricvehicles.id == 0 and another page should appear. But I ran into a problem that when I delete the data, they still remain on the screen and I need to revisit the page so that the data disappears. Tell me how can I do so that after deleting the data, the page is immediately updated and the data disappears and there is no need to revisit the page?

Widget _child(Size size, BuildContext context, double topPadding) {
    return BlocBuilder<MycarsCubit, MycarsState>(
      builder: (context, stateElectricvehicles) {
        final ElectricvehiclesCubit cubit =
            BlocProvider.of<ElectricvehiclesCubit>(context);

        if (stateElectricvehicles is MycarsInitial) {
          carNumber.text = stateElectricvehicles.number;
          carNumber.selection = TextSelection.fromPosition(
            TextPosition(offset: carNumber.text.length),
          );

          if (stateElectricvehicles.id == 0) {
            savedCarId = stateElectricvehicles.id;
          }

          Future.delayed(const Duration(seconds: 3), () {
            if (mounted) {
              setState(() {
                isLoading = false;
              });
            }
          });

          final SharedPrefs prefs = SharedPrefs();
          return FutureBuilder<int?>(
            future: prefs.getInt('USER_ID'),
            builder: (context, snapshot) {
              
              if (!snapshot.hasData) {
                return const SizedBox();
              }

              return Padding(
                padding: EdgeInsets.only(top: topPadding + 10),
                child: Column(
                  children: [
                    Padding(
                      padding: const EdgeInsets.only(left: 30, right: 30),
                      child: BackstepWidget(
                        text: 'My EVs',
                        onBackPressed: () {
                          Routemaster.of(context).history.back();
                        },
                      ),
                    ),
                    const SizedBox(height: 10),
                    snapshot.data != null
                        ? CarList(
                            savedCarId: savedCarId,
                            onIndexChanged: (value) {
                              savedCarId = value;
                            },
                          )
                        : const SizedBox(),
                    const SizedBox(height: 30),
                    stateElectricvehicles.id == 0
                        ? isLoading
                            ? const CircularProgressIndicator(color: constants.Colors.purpleMain)
                            : const EmptyDetails()
                        : snapshot.data != null
                            ? Expanded(
                                child: SingleChildScrollView(
                                  child: Column(
                                    children: [
                                                      GestureDetector(
                                                        onTap: () {
                                                          showDialog(
                                                            context: context,
                                                            builder: (context) {
                                                              return RemoveCarDialog(
                                                                removed:
                                                                    (value) {
                                                                  if (value) {
                                                                    setState(
                                                                        () {
                                                                      stateElectricvehicles
                                                                              .carModel =
                                                                          null;
                                                                      stateElectricvehicles
                                                                          .id = 0;
                                                                      stateElectricvehicles
                                                                          .number = '';
                                                                      stateElectricvehicles
                                                                          .type = '';
                                                                    });
                                                                  }
                                                                },
                                                                state:
                                                                    stateElectricvehicles,
                                                              );
                                                            },
                                                          ).then((value) {
                                                            cubit.fetchCars(
                                                              snapshot.data ??
                                                                  -1,
                                                            );
                                                            final MycarsCubit
                                                                myCars =
                                                                BlocProvider.of<
                                                                        MycarsCubit>(
                                                                    context);
                                                            myCars.clear();
                                                          });
                                                        },

cubit

class MycarsCubit extends Cubit<MycarsState> {
  MycarsCubit()
      : super(MycarsInitial(
          model: '',
          number: '',
          type: '',
          id: 0,
        ));
  String number = '';
  String type = '';
  int id = 0;
  late CarModel? carModel;

  void clear() {
    number = '';
    type = '';
    id = 0;
    carModel = null;
  }

  void change({
    required String numberText,
    required String typeText,
    required int idText,
    required CarModel? carModelNew,
  }) {
    id = idText;
    number = numberText;
    type = typeText;
    carModel = carModelNew;
    emit(MycarsInitial(
      number: number,
      type: type,
      id: id,
      carModel: carModel,
    ));
  }
}

state

abstract class MycarsState {}

class MycarsInitial extends MycarsState {
  int id;
  String number;
  String type;
  CarModel? carModel;

  MycarsInitial({
    required this.id,
    required this.number,
    this.carModel,
    required this.type,
  });
}

Upvotes: 0

Views: 62

Answers (1)

Ozan Taskiran
Ozan Taskiran

Reputation: 3572

You have to emit the new state, you just change the variable inside your Cubit class.

Try to emit inside your clear:

 void clear() {
    emit(MycarsInitial(
          number: "",
          type: "",
          id: 0,
          carModel: null,
        ));
  }

Upvotes: 1

Related Questions