AppGallery Connect Cloud Functions Flutter
Hi everyone , Today I try describing how we can use AppGallery Connect Cloud Functions Flutter Plugin.
About AGC Cloud Functions
Cloud Functions enables serverless computing. It provides the Function as a Service (FaaS) capabilities to simplify app development and O&M by splitting service logic into functions and offers the Cloud Functions SDK that works with Cloud DB and Cloud Storage so that your app functions can be implemented more easily. Cloud Functions automatically scales in or out functions based on actual traffic, freeing you from server resource management and helping you reduce costs.
Setting up AppGallery Connect
Creating Your Project and Adding an App
If you have not added a project, sign in to AppGallery Connect, create a project, and add an app to the project. For details, please refer to Getting Started with Flutter.
Enabling Cloud Functions
You need to enable Cloud Functions before using it.
- Click My projects on APPGallery Connect.
- Find your project for which you need to enable Cloud Functions from the project list.
- Go to Build > Cloud Functions. The Cloud Functions page is displayed. Click Enable now in the upper right corner.
Integrating Cloud Functions with Flutter
- Add dependencies to the pubspec.yaml file of the Flutter project.
dependencies:
agconnect_cloudfunctions: ^1.2.0+300
2. Run the following command on the Terminal page or click Pub get in Android Studio to add dependencies.
flutter pub get
Now, you can use required Cloud Functions services in Flutter. We will use the demo application in the next steps. You can find it here.
Creating a Function
- You need to create a function in AppGallery Connect and add the function code.
2. Enter a name and description of the function in Name and Description, respectively.
Cloud Functions handler.js
If we want to use it with parameters, we need to edit the file.
Cloud Functions with parameter handler.js
3. Click Save.
Creating an HTTP Trigger
We will use HTTP Trigger in our example.
- On the function list page, click the name of a function to go to the function details page. If you click the function alias, the page for configuring the function alias will be displayed. Click Add Trigger on the Configure tab page. The page for adding a trigger is displayed.
2. In the config trigger area, set Trigger Type: HTTP, Method: POST, and Authentication Type: API client authentication (applicable to clients), URI Decode: Enable
3. Click Add.
4. Cloud Functions trigger identifier name
Calling a Function
We can call the functionCallable.call() method without parameters. We get the return of the function we wrote in AppGallery Connect.
Let’s examine our example through the demo. We should arrange the textbox according to the function name. Let’s call the function by clicking the “Call the Function” button.
We get the return of the function we wrote in AppGallery Connect.
Calling a Function with input parameters
Update Function in AppGallery Connect with handler.js. This function returns the sum of the 2 numbers it receives.
We can call the functionCallable.call() method with parameters.
Let’s call the function by clicking the “Call the Function” button.
Conclusion
In this article,I tried to explain the Flutter AGC Cloud Functions plugin with examples. If you have any questions, you can write the question in the comment section.