Flutter
Welcome to the Flutter SDK Installation Guide. This document provides step-by-step instructions for installing and configuring the Ditto SDK, enabling you to sync, query, and insert data seamlessly within your existing Flutter application.
If you're looking to get started quickly with an existing app we recommend following the Flutter Quick Start.
Before you begin, ensure you have the following installed:
Installed code editor (preferably Visual Studio Code or Android Studio)
Flutter SDK installed on your machine
An existing Flutter application to integrate Ditto into
Ditto Portal account with a Ditto App (see Getting Playground Token Credentials)
For Android development:
Android Studio installed on your machine
A physical Android device or Android emulator
For iOS development:
macOS with the latest version of Xcode installed
A physical iOS device or an iOS simulator
Run the following command in your terminal from the root of your application:
For iOS development, add the following permissions to ios/Runner/Info.plist:
For Android development, add the following permissions to android/app/src/main/AndroidManifest.xml.
Log in to your Ditto Portal account.
Navigate to your application and obtain the App ID and Playground Token. (See Getting Playground Token Credentials for details)
Locate the appID and playground token for your application. These are required to initialize the Ditto Live SDK.
Import the SDK in your Dart file where you plan to use it.
Initialize the SDK to obtain an instance of Ditto. This is the main entry point into all Ditto-related functionality.
To insert data into the Ditto store, use the following example. This demonstrates inserting a document into a collection named items:
Call the insertData function wherever appropriate in your application, such as in a button press handler.
To observe changes in the data store, register an observer. This example sets up an observer to listen for changes in the items collection:
Call the observeData function, typically in the initState method of a stateful widget to start observing when the widget is initialized.
Remember to cancel your observer in your widget’s dispose() method to prevent resource leaks
To keep your data in sync across devices and with the Ditto Cloud (Big Peer), you need to register sync subscriptions. Sync subscriptions define the specific data that should be automatically synchronized to the device.
To start sync on the device you need to call startSync. This enables this device to sync data with other peers including the Ditto Cloud.
Register a subscription to a collection to keep the data in sync. Below is an example of how to register a subscription to the items collection:
Call the syncData function, typically when initializing your Ditto instance.
Once your application is syncing data using Ditto, you can deploy it to multiple local devices, Android emulators, or iOS simulators. You can then disable the internet to observe the devices syncing offline.
- Ensure that all dependencies are up-to-date by updating to the latest version.