首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在删除数据而不重新访问的情况下刷新页面?

如何在删除数据而不重新访问的情况下刷新页面?
EN

Stack Overflow用户
提问于 2022-08-29 20:15:24
回答 1查看 37关注 0票数 0

我有一个包含来自服务器的数据的页面。我通过BloC接收数据。我还有一个按钮来删除数据。当我删除数据时,应该是stateElectricvehicles.id == 0,另一个页面应该会出现。但是我遇到了一个问题,当我删除数据时,它们仍然在屏幕上,我需要重新访问页面,这样数据就消失了。告诉我如何才能在删除数据之后,立即更新页面,使数据消失,而不需要重新访问该页?

代码语言:javascript
复制
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

代码语言:javascript
复制
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,
    ));
  }
}

状态

代码语言:javascript
复制
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,
  });
}
EN

回答 1

Stack Overflow用户

发布于 2022-08-29 20:22:55

你必须发出新的状态,你只需改变你的Cubit类中的变量。

试着在你的净空中发出:

代码语言:javascript
复制
 void clear() {
    emit(MycarsInitial(
          number: "",
          type: "",
          id: 0,
          carModel: null,
        ));
  }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73534216

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档