事件和肘节通讯
在为我的一个集团构建逻辑时,我面临着挑战。我有一个使用我的预订服务的集团,该服务反过来对服务器进行 API 调用,并根据收到的响应发出一些状态。然而,此服务需要一个动态的过滤器参数,并且是我的过滤器肘部发出的状态的结果。如何在我的过滤肘节和我的预订活动之间建立通信。我的伪代码如下所示。
class BookingsBloc{
onGetBookingsEvent(GetBookings event, Emitter<BookingState> emit)
{
final bookingContent=bookingService.getBookings(event.bookingFilter);
}
}
class GetBookingsEvent{
Map<String,dynamic> bookingsFilter;
GetBookings(this.bookingsFilter)
{
//The above booking filter should come from my filter cubit state.
}
}
//过滤器状态看起来像这样。
class FilterState extends Equatable {
bool activeOnly;
dynamic filterByIdentifier;
List<String>? orderTypes;
String? loadPort;
String? dischargePort;
String? customerReferenceNumber;
String? fromDate;
String? toDate;
bool submittedSI;
bool draftSI;
FilterState(
{this.activeOnly = true,
this.filterByIdentifier,
this.orderTypes,
this.loadPort,
this.dischargePort,
this.customerReferenceNumber,
this.fromDate = "",
this.toDate = "",
this.submittedSI = false,
this.draftSI = false});
FilterState copyWith({
activeOnly,
filterByIdentifier,
orderTypes,
loadPort,
dischargePort,
customerReferenceNumber,
fromDate,
toDate,
submittedSI,
draftSI,
}) {
return FilterState(
activeOnly: activeOnly ?? this.activeOnly,
filterByIdentifier: filterByIdentifier ?? this.filterByIdentifier,
orderTypes: orderTypes ?? this.orderTypes,
loadPort: loadPort ?? this.loadPort,
dischargePort: dischargePort ?? this.dischargePort,
customerReferenceNumber:
customerReferenceNumber ?? this.customerReferenceNumber,
fromDate: fromDate ?? this.fromDate,
toDate: toDate ?? this.toDate,
submittedSI: this.submittedSI,
draftSI: this.draftSI);
}
Map<String, dynamic> toMap() {
return {
"activeOnly": activeOnly,
"filterByIdentifier": filterByIdentifier,
"orderTypes": orderTypes,
"loadPort": loadPort,
"dischargePort": dischargePort,
"customerReferenceNumber": customerReferenceNumber,
"fromDate": fromDate,
"toDate": toDate,
"submittedSI": submittedSI,
"draftSI": draftSI
};
}
如何在预订事件类中监听正确的状态,或者是否有更好的方法来做到这一点。任何帮助将不胜感激。
I am facing a challenge while constructing a logic for one of my blocs. I have a bloc which makes use of my booking service which in turn makes an API call to the server and emit few states based on the response recieved. However this service needs a filter parameter which is dynamic and is a result of a state emitted by my filter cubit. How do i establish a communication between my filter cubit and my bookings event. My pseudo code looks like this.
class BookingsBloc{
onGetBookingsEvent(GetBookings event, Emitter<BookingState> emit)
{
final bookingContent=bookingService.getBookings(event.bookingFilter);
}
}
class GetBookingsEvent{
Map<String,dynamic> bookingsFilter;
GetBookings(this.bookingsFilter)
{
//The above booking filter should come from my filter cubit state.
}
}
//Filter State looks like this.
class FilterState extends Equatable {
bool activeOnly;
dynamic filterByIdentifier;
List<String>? orderTypes;
String? loadPort;
String? dischargePort;
String? customerReferenceNumber;
String? fromDate;
String? toDate;
bool submittedSI;
bool draftSI;
FilterState(
{this.activeOnly = true,
this.filterByIdentifier,
this.orderTypes,
this.loadPort,
this.dischargePort,
this.customerReferenceNumber,
this.fromDate = "",
this.toDate = "",
this.submittedSI = false,
this.draftSI = false});
FilterState copyWith({
activeOnly,
filterByIdentifier,
orderTypes,
loadPort,
dischargePort,
customerReferenceNumber,
fromDate,
toDate,
submittedSI,
draftSI,
}) {
return FilterState(
activeOnly: activeOnly ?? this.activeOnly,
filterByIdentifier: filterByIdentifier ?? this.filterByIdentifier,
orderTypes: orderTypes ?? this.orderTypes,
loadPort: loadPort ?? this.loadPort,
dischargePort: dischargePort ?? this.dischargePort,
customerReferenceNumber:
customerReferenceNumber ?? this.customerReferenceNumber,
fromDate: fromDate ?? this.fromDate,
toDate: toDate ?? this.toDate,
submittedSI: this.submittedSI,
draftSI: this.draftSI);
}
Map<String, dynamic> toMap() {
return {
"activeOnly": activeOnly,
"filterByIdentifier": filterByIdentifier,
"orderTypes": orderTypes,
"loadPort": loadPort,
"dischargePort": dischargePort,
"customerReferenceNumber": customerReferenceNumber,
"fromDate": fromDate,
"toDate": toDate,
"submittedSI": submittedSI,
"draftSI": draftSI
};
}
How can the correct state be listened inside the bookings event class, or is there a better way to do this. Any help will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你可以使用 BlocListener 来解决这个任务。 https://bloclibrary.dev/#/flutterbloccoreconcepts?id=bloclistener
它可以监听您的状态并做出反应。此外,尝试将任务划分为子任务,并在架构中使用单一责任原则。 https://www. digitalocean.com/community/conceptual_articles/solid-the-first- Five-principles-of-object-orient-design
另请注意,同时使用 Bloc 和 Cubit 看起来很奇怪。通常使用两者之一。
I think you can use BlocListener for resolving this task. https://bloclibrary.dev/#/flutterbloccoreconcepts?id=bloclistener
It can listen your state and react to it. In addition, try to divide the task into subtasks and use the single responsibility principle in your architecture. https://www.digitalocean.com/community/conceptual_articles/s-o-l-i-d-the-first-five-principles-of-object-oriented-design
Note also that it looks strange to simultaneously use Bloc and Cubit. Usually use one of the two.