Flutter
Parse SDK (REST)

Cloud Code Functions

18min

Using Cloud Functions in a Flutter App

Introduction

For complex apps, sometimes you just need a bit of logic that isn’t running on a mobile device. Cloud Code makes this possible.

Cloud Code is easy to use because it’s built on the same Parse JavaScript SDK that powers thousands of apps. The only difference is that this code runs in your Parse Server rather than running on the user’s mobile device.

You can use Cloud Code to offload processing to the Parse servers thus increasing your app’s perceived performance. You can create hooks that run whenever an object is saved or deleted. This is useful if you want to validate or sanitize your data. You can also use Cloud Code to modify related objects or kick off other processes such as sending off a push notification.

When you update your Cloud Code, it becomes available to all mobile environments instantly. You don’t have to wait for a new release of your application. This lets you change app behavior on the fly and add new features faster.

This section explains how to create and deploy Cloud Code, followed by how to call a cloud function in Flutter projects through Back4App.

In this guide, the focus is to demonstrate the use of Cloud Function through Flutter. You can find more in-depth information in Parse Official Cloud Code Documentation.

Prerequisites

To complete this tutorial, you will need:

Goal

Run Parse Cloud Code on Back4App from a Flutter App.

1 - Create a Cloud Code File

  1. Go to your App at Back4App Website and click on Server Settings.
  2. Find the Cloud Code and click on Functions & Web Hosting. It looks like this:
Document image


3. Upload or create a new file (you can also edit the currentmain.jsfile directly on the browser). Then, click at Deploy as shown here:

Document image


Yourmain.jsfile should look like this:

JS


You pass parameters to your Cloud function from your Flutter App and access then within the request.params object.

2 - Understanding the ParseCloudFunction class

The ParseCloudFunction class defines provides methods for interacting with Parse Cloud Functions.

A Cloud Function can be called with ParseCloudFunction.execute({parameters: params}) that returns a map object or ParseCloudFunction.executeObjectFunction<>({parameters: params}) that returns a ParseObject.

Parameters are optional and a map object is expected.

3 - Call Parse Cloud function

Now that you have deployed the Cloud Functions, we can call the functions using Flutter.

Example 1 - Call a Cloud Function and get the return

JS


The result displayed in the console will be:

1

Example 2 - Call a Cloud Function with parameters and get the return

JS


The result displayed in the console will be:

flutter: Hello from Cloud Code!

Example 2.1 - Call a Cloud Function with parameters and get the return

JS

flutter: 30

Example 3 - Call a Cloud Function with parameters and get ParseObject on return

JS


The result displayed in the console will be:

flutter: {"className":"ToDo","objectId":"H0kHsIr6KT","createdAt":"2021-06-25T00:21:10.023Z","updatedAt":"2021-06-25T00:21:10.023Z","title":"Task 1","done":false}

Example 4 - Call a Cloud Function that returns a list of maps that can be converted to a ParseObject

JS


The result displayed in the console will be:



5 - Call Cloud Function from Flutter

Let’s now use our example call cloud Function in Flutter App, with a simple interface.

Open your Flutter project, go to the main.dart file, clean up all the code, and replace it with:

JS


Find your Application Id and Client Key credentials navigating to your app Dashboard at Back4App Website.

Update your code in main.dart with the values of your project’s ApplicationId and ClientKey in Back4app.

  • keyApplicationId = App Id
  • keyClientKey = Client Key

Run the project, and the app will load as shown in the image.

Document image


Conclusion

At this stage, you are able to code and call your own Cloud Code in your Flutter App using Parse Server Core features through Back4App!.

Updated 24 Jul 2024
Did this page help you?