DEV Community

Zachary Powell
Zachary Powell

Posted on

1

Integrating Cloud Functions in a Flutter application

Cloud Functions enables serverless computing through Function as a Service (FaaS) capabilities to simplify app development and O&M. This helps you implement functions more simply and build service capabilities more quickly. The following describes how to integrate Huawei's AppGallery Connect Cloud Function service in Flutter.
We will create a basic new project as an example but of course you could make use of a project you already have thats using flutter!

Project Setup

Install the Flutter environment.

Download the Flutter SDK package and decompress the package to any directory.
Image description
Download the Flutter and Dart plugins in Android Studio.
Image description

Enable Huawei Cloud Functions

Create an Android app in AppGallery Connect and enable Cloud Functions for it as detailed in this guide.
Now we can go ahead and create a cloud function as you require it use this guide to help understand what can be done.
Once your happy with your function (in this instance it might just be as simple as returning a hello world statement for testing) you can go ahead and create a HTTP trigger that will trigger the function.

Create Flutter Project

Next create your flutter project in android studio (or open your existing project if you already have one!).
Image description
Download your agconnect-services.json file from the AppGallery Connect console and add it into your project under the 'app' directory
Image description

Within the project level build.gradle file make sure to include the huawei maven repo and add the agcp package as a dependency.

buildscript {
repositories {
google()
mavenCentral()
maven { url 'https://developer.huawei.com/repo/' }
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.4'
classpath 'com.huawei.agconnect:agcp:1.6.2.300'
}
}
view raw build.gradle hosted with ❤ by GitHub

Next in your app level build.gradle apply the agconnect plugin as so:

apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
apply plugin: 'com.huawei.agconnect'
view raw build.gradle hosted with ❤ by GitHub

Then in your pubspec.yaml file add the Cloud Function SDK dependency.

dependencies:
flutter:
sdk: flutter
agconnect_cloudfunctions: 1.2.0+300
view raw pubspec.yaml hosted with ❤ by GitHub

Call the Cloud function

The below code is an example of how you can call a simple cloud function and get the result returned.

FunctionCallable functionCallable = FunctionCallable("test-\$latest");
Map<String, dynamic> parameters = <String, dynamic>{
'year': _functionTextController.text
};
FunctionResult functionResult = await functionCallable.call(parameters);
view raw file.dart hosted with ❤ by GitHub

Where test-\$latest is the HTTP trigger identifier.

Sentry blog image

The countdown to March 31 is on.

Make the switch from app center suck less with Sentry.

Read more

Top comments (0)

Sentry mobile image

Improving mobile performance, from slow screens to app start time

Based on our experience working with thousands of mobile developer teams, we developed a mobile monitoring maturity curve.

Read more