'Subscription with Phoenix in Flutter. How to get response from subscription
What happened?
When I click on the button confirm, a method _doSend is called. This method call a HTTP POST request (the method sends a transaction to a blockchain and waits the result). HTTP POST response contains the status of the response (“pending”) and we get the “ok” status with the Subscription with Phoenix. So when I receive “ok”, I want to display the status. With the following code, my subcription is ok:
logs from HTTP request and response:
[log] sendTx: requestHttp.body={"version":1,"address":"0…c62d75a51d8c53187fc1f3f50ed885666c60b17941a3f0e"}
[log] sendTx: responseHttp.body={"status":"pending","tra…1794E1A1D69C4184B3D3D58C823E778549F139361F1C0ED"}
The status is pending => ok
The status is ok => ok
BUT I don't find how to get the status from subscription ....
My Code:
/// SPDX-License-Identifier: AGPL-3.0-or-later
// Dart imports:
import 'dart:async';
// Flutter imports:
import 'package:aewallet/ui/views/transactions/phoenix_link.dart';
import 'package:flutter/material.dart';
// Package imports:
import 'package:aeuniverse/ui/widgets/components/buttons.dart';
import 'package:core/localization.dart';
import 'package:core/service/app_service.dart';
import 'package:core/util/get_it_instance.dart';
import 'package:core_ui/ui/util/dimens.dart';
// Project imports:
import 'package:graphql_flutter/graphql_flutter.dart';
class TransferConfirmSheet extends StatefulWidget {
const TransferConfirmSheet({super.key});
@override
State<TransferConfirmSheet> createState() => _TransferConfirmSheetState();
}
class _TransferConfirmSheetState extends State<TransferConfirmSheet> {
PhoenixLink? link;
ValueNotifier<GraphQLClient>? client;
// your subscription query
final subscriptionDocument = gql(
"""subscription { transactionConfirmed(address: "0000D2690135438EF7842075DCEB62E42F46614B6E3478FCE01A943EC7E699AC74C3") { nbConfirmations } }""");
Future<void> initGraphQLChannel() async {
// my absinthe plug api location
final HttpLink phoenixHttpLink = HttpLink(
'http://localhost:4000/socket/websocket',
);
// my websocket location
String websocketUri = "ws://localhost:4000/socket/websocket";
// creation of phoenixChannel for use in PhoenixLink
final phoenixChannel =
await PhoenixLink.createChannel(websocketUri: websocketUri);
final phoenixLink = PhoenixLink(
channel: phoenixChannel,
);
var _link = Link.split(
(request) => request.isSubscription, phoenixLink, phoenixHttpLink);
client = ValueNotifier(
GraphQLClient(
link: _link,
cache: GraphQLCache(),
),
);
}
@override
Widget build(BuildContext context) {
return SafeArea(
minimum:
EdgeInsets.only(bottom: MediaQuery.of(context).size.height * 0.035),
child: Column(
children: <Widget>[
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
client == null
? const SizedBox()
: Center(
child: GraphQLProvider(
client: client,
child: Subscription(
options: SubscriptionOptions(
document: subscriptionDocument,
),
builder: (result) {
if (result.hasException) {
return Text(result.exception.toString());
}
if (result.isLoading) {
return const Center(
child: CircularProgressIndicator(),
);
}
return ResultAccumulator.appendUniqueEntries(
latest: result.data,
builder: (context, {results}) =>
Text(result.data.toString()));
}),
),
),
],
),
),
Container(
margin: const EdgeInsets.only(top: 10.0),
child: Column(
children: <Widget>[
Row(
children: <Widget>[
AppButton.buildAppButton(
const Key('confirm'),
context,
AppButtonType.primary,
AppLocalization.of(context)!.confirm,
Dimens.buttonTopDimens,
onPressed: () async {
_doSend();
},
),
],
),
Row(
children: <Widget>[
AppButton.buildAppButton(
const Key('cancel'),
context,
AppButtonType.primary,
AppLocalization.of(context)!.cancel,
Dimens.buttonBottomDimens, onPressed: () {
Navigator.of(context).pop();
}),
],
),
],
),
),
],
),
);
}
Future<void> _doSend() async {
await initGraphQLChannel();
await sl.get<AppService>().send();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|