gpt4 book ai didi

flutter getx web sockets - 将流数据级联到提供者和 Controller

转载 作者:行者123 更新时间:2023-12-05 01:56:47 28 4
gpt4 key购买 nike

目标很简单

  • flutter 应用程序通过 websockets 调用 graphql api
  • 应用 View 调用 Controller , Controller 调用提供者,提供者通过 websockets 或 HTTP api 套接字调用调用 AWS appsync api
  • 我们不时从后端通过 websockets 接收来自 appsync api 或 HTTP api 套接字调用的数据流
  • streams 需要级联回 provider ,然后再到 controller(这是关键步骤)
  • Controller (不是提供者)将更新 obs 或 react 变量,使 UI 反射(reflect)更改

问题:数据是通过调用方的 websockets 接收的,但从未作为流返回给提供者或 Controller 以反射(reflect)更改

示例代码

实际来电者订单数据.dart

  @override
Stream<dynamic> subscribe({
String query,
Map<String, dynamic> variables,
}) async* {
debugPrint('===->subscribe===');
// it can be any stream here, http or file or image or media
final Stream<GraphQLResponse<String>> operation = Amplify.API.subscribe(
GraphQLRequest<String>(
document: query,
variables: variables,
),
onEstablished: () {
debugPrint(
'===->subscribe onEstablished ===',
);
},
);

operation.listen(
(event) async* {
final jsonData = json.decode(event.data.toString());
debugPrint('===->subscription data $jsonData');
yield jsonData;
},
onError: (Object e) => debugPrint('Error in subscription stream: $e'),
);
}

在提供者中订单提供者.dart

  Stream<Order> orderSubscription(String placeId) async* {
debugPrint('===->=== $placeId');
subscriptionResponseStream = orderData.subscribe(
query: subscribeToMenuOrder,
variables: {"place_id": placeId},
);

subscriptionResponseStream.listen((event) async* {
debugPrint(
"===->=== yielded $event",
);
yield event;
});
debugPrint('===->=== finished');
}

在 Controller 中家庭 Controller .dart

  Future<void> getSubscriptionData(String placeId) async {
debugPrint('===HomeController->getSubscriptionData===');
OrderProvider().orderSubscription(placeId).listen(
(data) {
//this block is executed when data event is receivedby listener
debugPrint('Data: $data');
Get.snackbar('orderSubscription', data.toString());
},
onError: (err) {
//this block is executed when error event is received by listener
debugPrint('Error: $err');
},
cancelOnError:
false, //this decides if subscription is cancelled on error or not
onDone: () {
//this block is executed when done event is received by listener
debugPrint('Done!');
},
);
}

homeview 调用 homecontroller

最佳答案

尝试使用 map用于转换流:

 @override
Stream<dynamic> subscribe({
String query,
Map<String, dynamic> variables,
}) {
debugPrint('===->subscribe===');
// it can be any stream here, http or file or image or media
final Stream<GraphQLResponse<String>> operation = Amplify.API.subscribe(
GraphQLRequest<String>(
document: query,
variables: variables,
),
onEstablished: () {
debugPrint(
'===->subscribe onEstablished ===',
);
},
);

return operation.map((event) {
return json.decode(event.data);
});
}

// elsewhere

final subscription = subscribe(
query: 'some query',
variables: {},
);

subscription.listen(
(jsonData) {
debugPrint('===->subscription data $jsonData');
},
onError: (Object e) => debugPrint('Error in subscription stream: $e'),
);

关于flutter getx web sockets - 将流数据级联到提供者和 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69715472/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com