amazon-web-servicesgraphql-flutterflutter-aws-amplify

Difficulty setting up AWS Amplify GraphQL data subscriptions


I am following the official online guide as well as this tutorial on how to set up subscriptions to data. I will be giving my two unfruitful attempts in this issue.

I am using this configuration for both:

imports:

import 'dart:async';
import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
import 'package:amplify_flutter/amplify_flutter.dart';
import 'package:amplify_test/models/Blog.dart';
import 'package:flutter/material.dart';

pubspec.yaml deps:

  cupertino_icons: ^1.0.2
  amplify_auth_cognito: '<1.0.0'
  amplify_flutter: '<1.0.0'
  amplify_datastore: '<1.0.0'
  amplify_datastore_plugin_interface: '<1.0.0'

Guide version:

I am following the guide sequentially to the point in the hyperlink where he enters this import and all the errors go away:

import 'package:amplify_datastore_plugin_interface/amplify_datastore_plugin_interface.dart';

However, Android Studio warns that all the functionality is already in this import:

package:amplify_flutter/amplify_flutter.dart

By this stage, the tutor does not have any errors, but on mine, event.item is throwing an error:

The argument type 'Model' can't be assigned to the parameter type 'Blog'.

initstate:

@override
  void initState() {

    super.initState();
    
    // TODO: Add a subscription and observe the DataStore
    _subscription = Amplify.DataStore.observe(Blog.classType)
        .listen((SubscriptionEvent event) {
      print(event.eventType);
      switch (event.eventType) {
        case EventType.create:
          _blogs.add(event.item);
          break;
        case EventType.update:
          var index = _blogs.indexOf(event.item);
          _blogs[index] = event.item;
          break;
        case EventType.delete:
          _blogs.removeWhere((element) => element.id == event.item.id);
          break;
      }
      setState(() {});
    });
    initBlogs();
  }

AWS Amplify version:

The supplied example uses a subscribe function to provide the functionality to the rest of the program. I incorporated it in the initstate so as to initialize the subscription (hopefully) once.

However, it cannot seem to find any functions related to Model, such as ModelSubscriptions, ModelQueries and ModelMutations. I would assume I am missing a pub dependency or something changed in future versions of the api?

initState:

@override
  void initState() {

    super.initState();
    Stream<GraphQLResponse<Blog>> subscribe() {
      final subscriptionRequest = ModelSubscriptions.onCreate(Blog.classType);
      final Stream<GraphQLResponse> operation = Amplify.API
          .subscribe(
        subscriptionRequest,
        onEstablished: () => print('Subscription established'),
      )
      // Listens to only 5 elements
          .take(5)
          .handleError(
            (error) {
          print('Error in subscription stream: $error');
        },
      );
      return operation;
    }

ModelSubscriptions is not recognized by Android Studio and the suggested actions are not helpful. The AWS guide does not provide any help on this, or I am too new to Amplify to search the right issue.


Solution

  • Apparently, the solution here was to cast SubscriptionEvent as Blog, as SubscriptionEvent<Blog>, since it was taking the Model class instead.