Flutter Install Guide
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.
This guide is for integrating Ditto into an existing Flutter application. If you’re looking to get started quickly with a brand new Flutter app, follow the Flutter QuickStart.
Prerequisites
Before you begin, ensure you have the following installed:
- Code editor (preferably Visual Studio Code or Android Studio)
- Flutter SDK (version 3.19.0 - 3.24.5)
- An existing Flutter application to integrate Ditto into
- Ditto Portal account with a Ditto App (see Getting SDK Connection Details)
Android
- Android Studio installed on your machine
- A physical Android device or Android emulator
- Upgrade your Kotlin Gradle version to
1.9.20
or later- Navigate to the root folder
- Open
./android/settings.gradle
- Update the
org.jetbrains.kotlin.android
plugin to1.9.20
iOS
- macOS with the latest version of Xcode installed
- A physical iOS device or an iOS simulator
Web
- Ensure you have Flutter SDK version 3.22.0 - 3.24.5 installed
- Follow the guide for Web Browser Support
Step 1: Add the Ditto Dependency
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
.
Step 2: Obtain Ditto Credentials
Log in to your Ditto Portal account.
Navigate to your application and obtain the App ID and Playground Token. (See Getting SDK Connection Details for details)
Locate the appID
and playground token
for your application. These are required to initialize the Ditto SDK.
Step 3: Import & Initialize Ditto
Import the SDK in your Dart file where you plan to use it.
Import these libraries to request device permissions.
Initialize the SDK to obtain an instance of Ditto
. This is the main entry point into all Ditto-related functionality.
Step 4: Creating and Reading Data
Insert Data
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.
Obseve Data Store Changes
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
Step 5: Syncing Data
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.
Starting Sync
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.
Registering a Subscription
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.
Syncing Data Offline
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.
Step 6: Web Browser Support
Web Assets
When Ditto is used on the web, it needs to download and initialize assets for
its WebAssembly binary. This happens automatically when calling await Ditto.init()
. By default, assets are bundled with the application and loaded
from the same web server that serves the application. After running flutter build web
, you can find those in the
build/web/assets/packages/ditto_live/lib/assets
directory.
Alternatively, you can host the contents of that directory on a CDN or other web
server and configure Ditto to load assets from there. All contents of the
directory, excluding the ditto.wasm
file itself, must be reachable at the URL
given by the wasmShimUrl
parameter. The ditto.wasm
file can be served from a
separate URL given by the wasmUrl
parameter.
Configuration for loading all assets from a CDN:
Please consider your application’s loading UI while downloading the assets as it may take a few seconds to download and initialize the WebAssembly binary.
It is strongly recommended to serve assets using a web server or CDN that supports compression. This will significantly reduce the download size of the assets and thereby the loading time of your application.
Disable Peer to Peer
On the web, Ditto only eupports syncing with the Ditto cloud and does not directly connect to other devices. Make sure that your app only enables peer-to-peer transports on mobile platforms.
Considerations for Web
- Flutter for Web is designed to work seamlessly with the Ditto cloud. Due to browser restrictions, direct peer-to-peer synchronization with other devices is not supported.
- The web platform utilizes an in-memory Ditto store, meaning data is not retained across page reloads.
- Ditto’s Flutter SDK currently supports the default build mode for the web.
- When developing with Flutter’s development server, it is required to quit and restart the server after making changes to the source code of your app (support for hot restart is in development).
Step 7: Troubleshooting
- Ensure that all dependencies are up-to-date by updating to the latest version.
- Contact the Ditto team through Contact Us
Was this page helpful?