amplify_api 0.3.0-rc.2 icon indicating copy to clipboard operation
amplify_api: ^0.3.0-rc.2 copied to clipboard

The Amplify Flutter API category plugin, supporting GraphQL and REST operations.

amplify_api #

The Amplify Flutter API category plugin.

Getting Started #

Visit our Web Site to learn more about AWS Amplify. #

Changes for version 0.3.0-rc.1 #

When creating subscriptions, now, a Stream object will be returned to you. This Stream will continue producing events until either the subscription encounters an error, or you cancel the subscription. In the case of await for, this cancellation occurs when breaking out of the loop.

Future<void> subscribe() async {
  final graphQLDocument = '''subscription MySubscription {
    onCreateBlog {
      id
      name
      createdAt
    }
  }''';
  final Stream<GraphQLResponse<String>> operation = Amplify.API.subscribe(
    GraphQLRequest<String>(document: graphQLDocument),
    onEstablished: () => print('Subscription established'),
  );

  try {
    // Retrieve 5 events from the subscription
    var i = 0;
    await for (var event in operation) {
      i++;
      print('Subscription event data received: ${event.data}');
      if (i == 5) {
        break;
      }
    }
  } on Exception catch (e) {
    print('Error in subscription stream: $e');
  }
}

Alternatively, you can call Stream.listen to create a StreamSubscription object which can be programmatically canceled.

Future<void> subscribe() async {
  final graphQLDocument = '''subscription MySubscription {
    onCreateBlog {
      id
      name
      createdAt
    }
  }''';
  final Stream<GraphQLResponse<String>> operation = Amplify.API.subscribe(
    GraphQLRequest<String>(document: graphQLDocument),
    onEstablished: () => print('Subscription established'),
  );
  final StreamSubscription<GraphQLResponse<String>> subscription =
      operation.listen(
    (event) {
      print('Subscription event data received: ${event.data}');
    },
    onError: (Object e) => print('Error in subscription stream: $e'),
  );

  // ...

  // Cancel the subscription and close the underlying stream.
  subscription.cancel();
}
16
likes
100
pub points
95%
popularity

Publisher

shield icon for verified publishersaws-amplify.com

The Amplify Flutter API category plugin, supporting GraphQL and REST operations.

Homepage
Repository (GitHub)
View/report issues

License

Apache-2.0 (LICENSE)

Dependencies

amplify_api_plugin_interface, amplify_core, flutter, plugin_platform_interface

More

Packages that depend on amplify_api