Declarative Library easy-to-use for building Flutter Applications in AWS

AWS Amplify

Amplify Flutter

AWS Amplify provides a declarative and easy-to-use interface across different categories of cloud operations. Our default implementation works with Amazon Web Services (AWS), but AWS Amplify is designed to be open and pluggable for any custom backend or service. See AWS Amplify for further details about the Amplify Framework.

AWS Amplify is a set of purpose-built tools and capabilities that allow frontend web and mobile developers to rapidly and easily build full-stack applications on AWS, with the flexibility to use the full range of AWS services as your use cases change.

We are iterating and looking for feedback and collaboration, so please let us know your feedback on our direction and roadmap.

For breaking changes from the developer preview versions please refer to this issue for migration details.

Supported Amplify Categories

  •  Authentication: APIs and building blocks for developers who want to create user authentication experiences with Amazon Cognito.
  •  Analytics: Easily collect analytics data for your app with Pinpoint. Analytics data includes user sessions and other custom events that you want to track in your app.
  •  Storage: Provides a simple mechanism for managing user content for your app in public, protected or private storage buckets with Amazon S3.
  •  DataStore: A programming model for leveraging shared and distributed data without writing additional code for offline and online scenarios, which makes working with distributed, cross-user data just as simple as working with local-only data.
  •  API (Rest): Provides a simple solution when making HTTP requests. It provides an automatic, lightweight signing process which complies with AWS Signature Version 4.
  •  API (GraphQL): Interact with your GraphQL server or AWS AppSync API with an easy-to-use & configured GraphQL client.

To Be Implemented

  •  Predictions
  •  Storage Hub Events (Listening to the Amplify Storage events)

Amplify for Flutter currently supports iOS and Android platforms.

Documentation

Flutter Development Guide

Amplify for Flutter is an open-source project and welcomes contributions from the Flutter community, see Contributing.

Prerequisites

Getting Started with Flutter app development and Amplify

  • Clone this repository
  • Install Amplify in a Flutter project
  • Add basic Amplify functionality to your project using one of the supported categories
  1. git clone [email protected]:aws-amplify/amplify-flutter.git
  2. Open your Flutter project. If you do not have an active Flutter project, you can create one after installing the Flutter development tooling and running flutter create <project-name> in your terminal.
  3. Using the Amplify CLI, run amplify init from the root of your project:

See Amplify CLI Installation

==> amplify init
Note: It is recommended to run this command from the root of your app directory
? Enter a name for the project helloAmplify
? Enter a name for the environment dev
? Choose your default editor: Visual Studio Code
? Choose the type of app that you\'re building flutter
Please tell us about your project
Only the following resource types are supported:
 * Auth
 * Analytics
 * Storage
 * API
? Where do you want to store your configuration file? ./lib/
  1. Add Amplify categories (choose defaults for this example):$ amplify add auth $ amplify add analytics
  2. Push changes to the cloud to provision the backend resources:$ amplify push
  3. In your pubspec.yaml file, add the following to dependencies:

Note: Do not include dependencies in your pubspec file that you are not using in your app. This can cause a configuration error in the underlying SDK.

dependencies:
  flutter:
    sdk: flutter
  amplify_flutter:
    path: /{path to your local amplify-flutter}/amplify-flutter/packages/amplify_flutter
  amplify_analytics_pinpoint:
    path: /{path to your local amplify-flutter}/amplify-flutter/packages/amplify_analytics_pinpoint
  amplify_auth_cognito:
    path: /{path to your local amplify-flutter}/amplify-flutter/packages/amplify_auth_cognito
  1. From the terminal run
flutter pub get
  1. In your main.dart file, add:
import 'package:flutter/material.dart';
import 'package:amplify_flutter/amplify.dart';
import 'package:amplify_analytics_pinpoint/amplify_analytics_pinpoint.dart';
import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';

import 'amplifyconfiguration.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool _amplifyConfigured = false;

  @override
  void initState() {
    super.initState();
  }

  void _configureAmplify() async {
    if (!mounted) return;

    // Add Pinpoint and Cognito Plugins
    Amplify.addPlugin(AmplifyAuthCognito());
    Amplify.addPlugin(AmplifyAnalyticsPinpoint());

    // Once Plugins are added, configure Amplify
    try {
      await Amplify.configure(amplifyconfig);
      setState(() {
        _amplifyConfigured = true;
      });
    } on AmplifyAlreadyConfiguredException {
      print(
          "Amplify was already configured. Looks like app restarted on android.");
    }

  }

  // Send an event to Pinpoint
  void _recordEvent() async {
    AnalyticsEvent event = AnalyticsEvent('test');
    event.properties.addBoolProperty('boolKey', true);
    event.properties.addDoubleProperty('doubleKey', 10.0);
    event.properties.addIntProperty('intKey', 10);
    event.properties.addStringProperty('stringKey', 'stringValue');
    Amplify.Analytics.recordEvent(event: event);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: const Text('Amplify example app'),
          ),
          body: ListView(padding: EdgeInsets.all(10.0), children: <Widget>[
            Center(
              child: Column (
                children: [
                  const Padding(padding: EdgeInsets.all(5.0)),
                  ElevatedButton(
                    onPressed: _amplifyConfigured ? null : _configureAmplify,
                    child: const Text('configure Amplify')
                  ),
                  ElevatedButton(
                    onPressed: _amplifyConfigured ? _recordEvent : null,
                    child: const Text('record event')
                  )
                ]
              ),
            )
          ])
      )
    );
  }
}

For iOS builds complete the following steps (from the root of your project):

  • rm ios/Podfile
  • flutter build ios
  • Modify the ios/Podfile and replace the second line with: platform :ios, '11.0'.

This ensures that your Flutter project is running the same ios version that the Amplify plugins are built on.

  1. From the root of your project, execute flutter run in the terminal.

Make sure that an Android or iOS device is already running; this can be a virtual device started from Android Studio.

Click Configure Amplify, then Record Event. From the terminal (in the root of your project) run amplify console analytics. This will open the Amazon Pinpoint console for your project in your default web browser. Within about a minute you should start seeing the events populating in the Events section of then Pinpoint console.

For further documentation and Amplify Category API usage, see the documentation.


Flutter and the related logo are trademarks of Google LLC. We are not endorsed by or affiliated with Google LLC.

Amplify Flutter Example

Sample flutter app for getting started with the Amplify Flutter Library. This example uses the Auth, Analytics, and Storage components of the Flutter library.

Please check out our docs here: https://docs.amplify.aws/start/q/integration/flutter

Getting Started

The app will not compile until you use the Amplify CLI to configure AWS resources necessary for running this app, or create your own amplifyconfiguration.dart file using the example in our documentation’.

This is because it does not contain an amplifyconfiguration.dart file necessary for connecting with AWS services.

You will need to use Amplify CLI to init the app and configure Analytics, Auth, and Storage. Please follow the instructions here:

https://docs.amplify.aws/start/getting-started/add-api/q/integration/flutter#setup-aws-cloud-resources-with-amplify-cli

For this example app you will also need to call amplify add auth, and amplify add storage with the Amplify CLI and call amplify push again.

Running these steps will generate the amplifyconfiguration.dart file within the lib folder.

Important Notes

This is a very basic app that interacts with AWS resources. We did not implement UI showing that the app is “loading” or “uploading” something from AWS. Some operations like logging in or uploading an image can take some time.

Please note when signing up that you MUST provide a country code for a new user’s phone number. For example, if your number is American, you will need to append +1 to the beginning.

GitHub

Source code: AWS Amplify.

SHARE Declarative Library easy-to-use for building Flutter Applications in AWS

You may also like...

Leave a Reply

Your email address will not be published.

Share