Integrating Cloud Functions in a Flutter application

Search This thread

devwithzachary

Inactive Recognized Developer
Oct 12, 2009
2,935
1,898
29
London
Huawei P40
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 that's using flutter!

Project Setup​

Install the Flutter environment.​

Download the Flutter SDK package and decompress the package to any directory.


0*jCxSOUrQc7b8ggZm.png

Download the Flutter and Dart plugins in Android Studio.


0*x3bfy97YRLy5xPKe.png

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 to use this guide to help understand what can be done.
Once you are 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!).


0*4Y7tSApXGeRjomaC.png

Download your agconnect-services.json file from the AppGallery Connect console and add it to your project under the ‘app’ directory


0*FJqv9Y7nsumfq654.png

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

Code:
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'
    }
}

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


Code:
apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
apply plugin: 'com.huawei.agconnect'

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

Code:
dependencies:
  flutter:
    sdk: flutter
    
  agconnect_cloudfunctions: 1.2.0+300

Call the Cloud function​

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

Code:
FunctionCallable functionCallable = FunctionCallable("test-\$latest");
Map<String, dynamic> parameters = <String, dynamic>{
  'year': _functionTextController.text
 };
FunctionResult functionResult = await functionCallable.call(parameters);

Where test-\$latest is the HTTP trigger identifier.
https://devwithzachary.medium.com/?...-------------------post_sidebar--------------