Flutter
...
Parse SDK (REST)
Data Objects

Data Types

26min

Parse Datatypes on Flutter

Introduction

In this guide you will learn about the Parse Datatypes using Flutter. You will read and save the Parse Datatythis on Back4App from a Flutter App.



Storing data on Parse is built around theParseObject. Each ParseObject contains key-value pairs of JSON-compatible data. This data is schemaless, which means that you don’t need to specify ahead of time what keys exist on each ParseObject. You can set whatever key-value pairs you want, and our backend will store them. For example, let’s say you’re tracking high scores for a game. A single ParseObject could contain:

1 score: 1337, playerName: "Sean Plott", cheatMode: false

Keys must be alphanumeric strings, and values can be:

  • String
  • Number
  • Bool
  • DateTime
  • Map
  • Null
  • List
  • File
  • Pointer
  • Relation
  • Geopoint

Each ParseObject has a class name that you can use to distinguish different sorts of data. For example, we could call the high score object a GameScore. There are also a few fields you don’t need to specify that are provided as a convenience:

  • objectId
  • createdAt

Each of these fields is automatically filled in by Back4app at the moment you save a new ParseObject.

We recommend you NameYourClassesLikeThis and nameYourKeysLikeThis, just to keep your code looking pretty.

Prerequisites

To complete this tutorial, you will need:

Understanding our App

To better understand Back4app in Flutter, we will create an application that will save and retrieve records with the main types of data supported. We won’t explain the Flutter application code once this guide’s primary focus is using the Flutter with Parse. The data types Pointer, Relation, File, Geopoint will be covered later in specific guides.

Let’s get started!

In the following steps, you will build an application that will save and read the data in the Back4App database.

1 - Create App Template

Open your Flutter project from the previous guide Flutter plugin for Parse Server.

Go to the main.dart file, clean up all the code, and replace it with:

Dart


Note: When debug parameter in function Parse().initialize is true, allows displaying Parse API calls on the console. This configuration can assist in debugging the code. It is advisable to disable debug in the release version.

2 - Connect Template to Back4app Project

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


3 - Code for Save Object

The create function will create a new object in Back4app database. Search for the function doSaveData in file main.dart, and insert the code below inside the doSaveData function:

Dart


To build this function, follow these steps:

  1. Make a new instance of the Parse DataTypes class with the command ParseObject("DataTypes").
  2. Use the set function to set the parameters for this object.
  3. Call the save function in ParseObject, which will effectively register the object to your database in the Back4app Dashboard.
  4. Display a message with the objectId of the saved object.

To test it, click on the Run button in Android Studio/VSCode. Click on the Save Data button. To confirm that the new object is in the database, you can access the Back4app Dashboard or the doReadData function code.

4 - Code for Read Object

The read function is responsible for querying the database and returning the object created in doSaveData function. Search for the function doReadData in the file main.dart, then insert the code below inside doReadData function:

Dart


To build this function, follow these steps:

  1. Create an instance ofParseQuery object for DataTypes class. Insert a condition in the query, to search for the object created in the doSaveData function using the value of the objectId.
  2. Do a Query’s search method using query() method.
  3. If the operations succeed, one object DataTypes will be returned.
  4. To access the values of the fields of our ParseObject use the get method, informing the data type.

To test it, click on the Run button in Android Studio/VSCode. First, click on the Save Data button e later click on Read Data button. Check the query result on the console.

5 - Code for Update Object and Special Methods

The update function is responsible for updating data in the object created on the doSaveData function. Search for the function doUpdateData in the file main.dart, and insert the code below inside the doUpdateData function:

Dart


To build this function, follow these steps:

  1. Make a new instance of the ParseDataTypes class with the command ParseObject("DataTypes").
  2. Use the set function to define objectId of the object that we want to update and the field that we want to update
  3. Call thesave function in ParseObject, which will effectively register the object to your database in the Back4app Dashboard.
  4. Display a message with theobjectId of the updated object.

To test it, click on the Run button in Android Studio/VSCode. First, click on the Save Data button e later click on Update Data button. To confirm that the updated object is in the database, you can access the Back4app Dashboard or you can execute the doReadData function.

6 - Using Counters

The above example contains a common use case. The intField field can be a counter that we’ll need to update continually. The above solution works, but it’s cumbersome and can lead to problems if you have multiple clients trying to update the same counter. Parse provides methods that atomically increment (or decrement) any number field to help with storing counter-type data. So, the same update can be rewritten as:

1 final parseObject = ParseObject("DataTypes") 2 ..objectId = objectId 3 ..setIncrement("intField", 1);

To decrement:

1 final parseObject = ParseObject("DataTypes") 2 ..objectId = objectId 3 ..setDecrement("intField", 1);

7 - Using Lists

Parse also provides methods to help in storing list data. There are three operations that can be used to change a list field atomically:

  • setAdd and setAddAll: append the given objects to the end of an array field.
  • setAddUnique and setAddAllUnique: add only the given objects which aren’t already contained in an array field to that field. The position of the insert is not guaranteed.
  • remove and removeAll: removes all instances of the given objects from an array field.

7.1 - Examples with setAdd and setAddAll

listStringField has the value:

["a","b","c","d","e","f","g","g"]

Running the code below:

Dart
Dart


After this command the result of thestringListfield will be:

["a","b","c","d","e","e","f","g","g"]

7.2 - Examples with setAddUnique and setAddAllUnique

listStringField has the value:

["a","b","c","d","e"]

Running the code below:

Dart
Dart


After this command the result of thestringListfield will be:

["a","b","c","d","e","f"]

No values were repeated.

7.3 - Examples with remove and removeAll

listStringField has the value:

["a","b","c","d","e","f"]

Running the code below:

Dart
Dart


After this command the result of the stringList field will be:

["a","b"]

Note that it is not currently possible to atomically add and remove items from an array in the same save. You will have to call the save for every different array operation you want to perform separately.

8 - Remove field from ParseObject

You can delete a single field from an object by using the unset operation:

Dart


It’s done!

At this point, you learned about the Datatypes available on Parse Server and used them through a Flutter app. You learned how to read and save this data, special functions and learned a little more about ParseObject.