Dart - 在将 RxCommand 结果发送到 RxLoader 之前处理它
Dart - processing RxCommand result before send it to RxLoader
我正在编写一个 Flutter 应用程序,我决定使用 RxDart
沿着管理器、服务和 UI 传递我的数据和事件。
基本上,我有一个从 Web 服务获取数据并 returns 的服务。让我们假设它 returns 一个名为 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));
}
}
现在在我的管理器中 class 我在 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"),
)),
)
]));
}
}
它的效果很好,但是当我想从网络服务加载下一页的数据并将其附加到列表时,我找不到存储前一页内容的解决方案,只能附加新页面的内容给他们,因为数据是沿着 RxCommand
和 RxLoader
自动传递的。
当loadExploreData
将响应发送给管理器时,我需要先将结果附加到一个列表,然后将该列表作为结果发送给RxLoader
。有什么建议吗?
嗯,这是一个很好的问题,如果仅使用 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;
};
}
}
我正在编写一个 Flutter 应用程序,我决定使用 RxDart
沿着管理器、服务和 UI 传递我的数据和事件。
基本上,我有一个从 Web 服务获取数据并 returns 的服务。让我们假设它 returns 一个名为 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));
}
}
现在在我的管理器中 class 我在 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"),
)),
)
]));
}
}
它的效果很好,但是当我想从网络服务加载下一页的数据并将其附加到列表时,我找不到存储前一页内容的解决方案,只能附加新页面的内容给他们,因为数据是沿着 RxCommand
和 RxLoader
自动传递的。
当loadExploreData
将响应发送给管理器时,我需要先将结果附加到一个列表,然后将该列表作为结果发送给RxLoader
。有什么建议吗?
嗯,这是一个很好的问题,如果仅使用 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;
};
}
}