什么时候创建新的集团?

独特的

我还在学习集团模式。我使用单个块创建了两个页面,ViewPage 和 DetailsPage。这是我的集团:

getRecordEvent
deleteRecordEvent
LoadedState
LoadedErrorState
DeletedState
DeletedErrorState

视图页面只会构建一个小部件,其中包含LoadedState. 当用户点击任何记录时,它会推送详细信息页面并显示带有删除按钮的详细记录。当用户按下删除按钮时,我会监听DeletedState并调用getRecord事件以使用更新的记录再次填充视图页面。一切正常,但我的问题是我在删除记录时遇到错误。当状态为 时DeleteErrorState,我的视图页面变为空,因为我没有getRecord在那里打电话,因为错误可能是互联网连接,并且将显示两个错误对话框。一个用于DeletedErrorStateLoadedErrorState

我知道这是 bloc 的默认行为。我必须创建一个单独的集团deleteRecordEvent吗?而且,如果我创建一个新页面来添加记录,这也是一个单独的块吗?

更新:

这是 ViewPage 的示例。按下按钮后,DetailsPage 只会调用 deleteRecordEvent。

ViewPage.dart

  void getRecord() {
        BlocProvider.of<RecordBloc>(context).add(
            getRecordEvent());
  }
  
  @override
  Widget build(BuildContext context) {
    return
      Scaffold(
      body: buildBody(),
      ),
    );
  }
  
  buildBody() {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: BlocConsumer<RecordBloc, RecordState>(
            listener: (context, state) {
              if (state is LoadedErrorState) {
                showDialog(
                    barrierDismissible: false,
                    context: context,
                    builder: (_) {
                      return (WillPopScope(
                          onWillPop: () async => false,
                          child: ErrorDialog(
                            failure: state.failure,
                          )));
                    });
              } else if (state is DeletedState) {
                Navigator.pop(context);
                getRecord();
                
              } else if (state is DeletedErrorState) {
                Navigator.pop(context);
                showDialog(
                    barrierDismissible: false,
                    context: context,
                    builder: (_) {
                      return (WillPopScope(
                          onWillPop: () async => false,
                          child: ErrorDialog(
                            failure: state.failure,
                          )));
                    });
              }
            },
            builder: (context, state) {
              if (state is LoadedState) {
                return Expanded(
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      state.records.length <= 0
                          ? noRecordWidget()
                          : Expanded(
                              child: ListView.builder(
                                  shrinkWrap: true,
                                  itemCount: state.records.length,
                                  itemBuilder: (context, index) {
                                    return Card(
                                        child: Padding(
                                      padding: EdgeInsets.symmetric(
                                          vertical: Sizes.s8),
                                      child: ListTile(
                                        title: Text(state.records[index].Name),
                                        subtitle: state.records[index].date,
                                        onTap: () {
                                           showDialog(
                                              barrierDismissible: false,
                                              context: context,
                                              builder: (_) {
                                                return BlocProvider<RecordBloc>.value(
                                                    value: BlocProvider.of<RecordBloc>(context),
                                                    child: WillPopScope(
                                                      onWillPop: () async => false,
                                                      child:
                                                          DetailsPage(record:state.records[index]),
                                                    ));
                                              });
                                        },
  
                                      ),
                                    ));
                                  }),
                            ),
                    ],
                  ),
                );
              }
              return (Container());
            },
          ),
      ),
    );
  }  
头骨

关于集团

作为一般经验法则,bloc每个ui. 当然,情况并非总是如此,因为这取决于几个因素,其中最重要的是您在ui. 对于您的情况,如果有一个ui将项目列表保存到 item-details 中ui,我将创建两个blocs. 一个将只处理加载项目(ItemsBloc例如),另一个将处理单个项目的操作 ( SingleItemBloc)。我现在可能只使用该delete事件,但随着应用程序的增长,我将添加更多事件。这一切都促进了关注点分离的概念。

将其应用于您的案例,SingleItemBloc将处理对单个项目的删除、修改、订阅等,同时ItemsBloc将处理从不同存储库(本地/远程)加载项目。

由于我没有您的代码,bloc我无法提供任何修改。

针对您的情况的解决方案

每次state发出新项目时,您似乎都会丢失项目列表的最后一个版本您应该保留从存储库中获取的最后一个列表的本地副本。如果出现错误,您只需使用该列表;如果不只是将新列表保存为您拥有的最后一个列表。

class MyBloc extends Bloc<Event, State> {
  .....
  List<Item> _lastAcquiredList = [];
  
  Stream<State> mapEventToState(Event event) async* {
      try {
        ....

        if(event is GetItemsEvent) {
          var newList = _getItemsFromRepository();
          yield LoadedState(newList);
          _lastAcquiredList = newList;
        }
       ....
      } catch(err) {
         yield ErrorState(items: _lastAcquiredItems);
      }
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章