【问题标题】:Dart - processing RxCommand result before send it to RxLoaderDart - 在发送到 RxLoader 之前处理 RxCommand 结果
【发布时间】:2020-01-18 14:19:30
【问题描述】:

我正在编写一个 Flutter 应用程序,我决定使用 RxDart 将我的数据和事件传递给管理器、服务和 UI。 基本上我有一个服务,它从 Web 服务中获取数据并返回它。假设它返回一个名为ExploreEntity 的模型的List

class ExploreMockService extends ExploreServiceStruct {
  final String response = /** a sample json **/;
  @override
  Future<List<ExploreEntity>> loadExploreData(PaginationInput input) async {
    await Future.delayed(new Duration(seconds: 2));
    return List<ExploreEntity>.from(jsonDecode(response));
  }
}

现在在我的经理类中,我在 RxCommand 中调用 loadExploreData 方法。

class ExploreManagerImplementation extends ExploreManager {
  @override
  RxCommand<void, List<ExploreEntity>> loadExploreDataCommand;

  ExploreManagerImplementation() {
    loadExploreDataCommand = RxCommand.createAsync<PaginationInput, List<ExploreEntity>>((input) =>
        sl //Forget about this part
            .get<ExploreServiceStruct>() //and this part if you couldn't understand it
            .loadExploreData(input));
  }


}

最后我通过RxLoader 得到结果,如果成功获取数据,则将其传递给GridView

class ExplorePageState extends State<ExplorePage>{
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Explore"),
        ),
        body: Column(children: <Widget>[
          Expanded(
            child: RxLoader<List<ExploreEntity>>(
                commandResults:
                sl.get<ExploreManager>().loadExploreDataCommand.results,
                dataBuilder: (context, data) => ExploreGridView(data),
                placeHolderBuilder: (context) => Center(
                  child: Center(
                    child: CircularProgressIndicator(),
                  ),
                ),
                errorBuilder: (context, error) => Center(
                  child: Text("Error"),
                )),
          )
        ]));
  }
}

它就像一个魅力,但是当我想从 Web 服务加载下一页的数据并将其附加到列表时,我找不到存储前一页内容的解决方案,而只是附加新页面的内容内容给他们,因为数据是通过RxCommandRxLoader自动传递的。

loadExploreData 将响应发送给经理时,我需要先将结果附加到一个列表中,然后将该列表作为结果发送给RxLoader。有什么建议吗?

【问题讨论】:

    标签: flutter dart reactive-programming rxdart


    【解决方案1】:

    嗯,如果仅使用 Rx 就可以做到这一点,这是一个很好的问题。我要做的是将收到的项目列表保存在管理器中。因此,当触发获取下一页的命令时,该命令将首先将新数据添加到列表中,然后将整个列表推送到 UI。 我很好奇是否有其他解决方案。

    我在粗略代码示例中描述的方法

    class ExploreManagerImplementation extends ExploreManager {
      List<ExploreEntity>> receivedData = <ExploreEntity>[];
    
      @override
      RxCommand<void, List<ExploreEntity>> loadExploreDataCommand;
    
      ExploreManagerImplementation() {
        loadExploreDataCommand = RxCommand.createAsync<PaginationInput, List<ExploreEntity>>((input) 
          async {
             var newData = await sl //Forget about this part
             .get<ExploreServiceStruct>() //and this part if you couldn't understand it
                .loadExploreData(input);
         receivedData.addAll(newData);
         return receivedData;
         };
      }
    
    
    }
    

    【讨论】:

    • 谢谢您,先生...我在想这个,但我无法实现它...您能提供一些代码吗?也是这样,我不得不放弃RxLoader?
    • 谢谢您,先生,您的回答帮助了我,并按我的需要工作......我注意到一个与问题无关的问题,但无论如何我还是会问你,看看你是否知道出了什么问题。 .. 我已经从RxLoader 中删除了placeHolderBuilder,因为每次我想加载新数据以列出并显示CircularProgressIndicator 时,我都不会隐藏列表。但无论如何它一直显示,即使我用Text 小部件替换它,但每次运行loadExploreDataCommand 时我都会得到CircularProgressIndicator。可能是RxLoader的bug?
    • 你能在 rx_command 存储库上提出一个关于这个的问题吗?也请在那里添加源代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    • 2017-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-11
    • 2018-04-13
    相关资源
    最近更新 更多