Flutter
...
Parse SDK (REST)
Data Objects

Data Types

18min

This guide introduces Parse data types in Flutter, using examples from the main.dart file. Parse data storage revolves around ParseObject, which contains key-value pairs of JSON-compatible data, making it schemaless. This means you can store any data without defining a schema in advance.

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 Parse Object could contain:

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

We suggest you NameYourClassesLikeThis and nameYourKeysLikeThis to enhance code readability and maintainability.

Understanding our App

To better understand Back4app, let's explore code examples of Parse operations in a Flutter application with the major supported data types. This guide won’t explain any Flutter app code as the primary focus is using Parse with Flutter.

Prerequisites

To complete this tutorial, you will need:

1 - Working with Parse Objects

Each ParseObject has a class name (e.g., GamePoint) used to distinguish different data types. Here’s how you can create and save a new Parse object with data types string, int, & boolean:

Dart


To query the new Parse object and retrieve the data types:

Dart


2 - Counters

You can increment or decrement an integer field in a ParseObject using the set() method.

However, this isn't effective and can lead to problems if multiple clients are trying to update the same counter. Parse provides two methods that automatically increment and decrement any number field to store counter-type data.

setIncrement()

setDecrement()

An update to increment a counter int value will be written as:

Dart


To decrement:

Dart


Using setIncrement() and setDecrement() with the save() call allows you to update a value as part of a larger save operation where you may be modifying multiple fields. This is better to avoid extra network requests.

3 - Lists

Parse provides methods to work with list data, including setAdd, setAddUnique, setRemove, and their respective All versions.

List: ["a","b","c"]

3.1 - setAdd

Dart


Result: ["a","b","c","d"]

3.2 - setAddAll

Dart


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

setAddAll does not add duplicate elements if the list already contains them.

3.3 - setAddUnique

Dart


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

3.4 - setRemove

Dart


Result: ["a","b","c","e","f","g"]

4 - Remove field from ParseObject

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

Dart


5 - Files

ParseFile lets you store and retrieve application files in the Cloud. Below is a basic example of uploading a local image file to Back4App:

Dart


This code snippet shows how to retrieve the image file:

Dart


Starting from Parse Server 5.2.3 they are breaking changes that could cause errors during an attempt to upload files. Follow this guide to fix any upload issues you may experience.

A later guide will further discuss and show templates on how to save and display Files with Parse.

6 - GeoPoint

Parse allows you to associate real-world latitude and longitude coordinates with an object with its GeoPoint data type. Adding a ParseGeoPoint to a ParseObject will enable queries to consider the proximity of an object to a reference point.

Example:

Dart


You can find a standalone example of saving Geopoint location in this main.dart file.

Full App Example

This example demonstrates how to:

  • Create, delete, and update a GamePoint Parse object.
  • Handle various data types, including strings, doubles, booleans, files, geopoint, lists, and dates.

On iOS, you will need to grant your simulator the necessary access permissions.

  • iOS provides a developer guide on protected resources. The example applications in this guide will require the Location and Photos permission keys found in that guide to work. Add the keys to the Info.plist file, located at /ios/Runner/Info.plist:

You can skip the above step if you are running your Flutter app on the web:

Dart


This code initializes the Parse SDK in Flutter, sets up the main application, and displays a simple home page with a title.

Conclusion

In this guide, you learned about ParseObjects and the various datatypes available to Parse. You also learned how to handle operations like saving and retrieving the datatypes to and from your back4app backend.