Install Guides
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 docid linde0fkzokginfhgo 8 prerequisites 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 docid\ u214qhyymcxbv9jnamrfh ) 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 step 1 add the ditto dependency to the application run the following command in your terminal from the root of your application flutter pub add ditto live for ios development, add the following permissions to ios/runner/info plist info plist \<key>nsbluetoothalwaysusagedescription\</key> \<string>uses bluetooth to connect and sync with nearby devices\</string> \<key>nsbluetoothperipheralusagedescription\</key> \<string>uses bluetooth to connect and sync with nearby devices\</string> \<key>nslocalnetworkusagedescription\</key> \<string>uses wifi to connect and sync with nearby devices\</string> \<key>nsbonjourservices\</key> \<array> \<string> http alt tcp \</string> \</array> for android development, add the following permissions to android/app/src/main/androidmanifest xml \<uses permission android\ name="android permission bluetooth" android\ maxsdkversion="30" /> \<uses permission android\ name="android permission bluetooth admin" android\ maxsdkversion="30" /> \<uses permission android\ name="android permission bluetooth advertise" tools\ targetapi="s" /> \<uses permission android\ name="android permission bluetooth connect" tools\ targetapi="s" /> \<uses permission android\ name="android permission bluetooth scan" android\ usespermissionflags="neverforlocation" tools\ targetapi="s" /> \<uses permission android\ name="android permission access fine location" android\ maxsdkversion="32" /> \<uses permission android\ name="android permission access coarse location" android\ maxsdkversion="30" /> \<uses permission android\ name="android permission internet" /> \<uses permission android\ name="android permission access wifi state" /> \<uses permission android\ name="android permission access network state" /> \<uses permission android\ name="android permission change network state" /> \<uses permission android\ name="android permission change wifi multicast state" /> \<uses permission android\ name="android permission change wifi state" /> \<uses permission android\ name="android permission nearby wifi devices" android\ usespermissionflags="neverforlocation" tools\ targetapi="tiramisu" /> step 2 obtain ditto appid and playground token log in to your https //portal ditto live navigate to your application and obtain the app id and playground token (see docid\ u214qhyymcxbv9jnamrfh for details) locate the appid and playground token for your application these are required to initialize the ditto live sdk step 3 import and initialize the ditto sdk import the sdk in your dart file where you plan to use it import 'package\ ditto live/ditto live dart'; initialize the sdk to obtain an instance of ditto this is the main entry point into all ditto related functionality future\<void> main() async { 	widgetsflutterbinding ensureinitialized(); 	 	// request device permissions 	await \[ permission bluetoothconnect, permission bluetoothadvertise, permission nearbywifidevices, permission bluetoothscan ] request(); // initialize ditto with your ditto appid and playground token final identity = await onlineplaygroundidentity create( appid "your app id", // replace with your specific id token "your playground token", // replace with your specific token ); final ditto = await ditto open(identity); // start ditto sync await ditto startsync(); runapp(myapp(ditto ditto)); } class myapp extends statelesswidget { final ditto ditto; const myapp({super key, required this ditto}); @override widget build(buildcontext context) { return placeholder(); } } step 4 creating and observing data store changes insert data into the ditto store to insert data into the ditto store, use the following example this demonstrates inserting a document into a collection named items future\<void> insertdata() async { final item = {'name' 'sample item', 'value' 100}; await ditto store execute( "insert into items documents (\ item)", arguments {"item" item}, ); } call the insertdata function wherever appropriate in your application, such as in a button press handler floatingactionbutton( onpressed insertdata, child icon(icons add), ) register an observer to listen for 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 future\<void> observedata() async { final observer = await ditto store registerobserver( "select from items", onchange (event) { 	 // handle data changed event 	 print("data changed"); 	 }, ); } call the observedata function, typically in the initstate method of a stateful widget to start observing when the widget is initialized @override void initstate() { super initstate(); observedata(); } remember to cancel your observer in your widget’s dispose() method to prevent resource leaks @override void dispose() { observer cancel(); super dispose(); } 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 await ditto startsync(); 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 future\<void> syncdata() async { await ditto sync registersubscription("select from items"); } call the syncdata function, typically when initializing your ditto instance step 6 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 7 troubleshooting ensure that all dependencies are up to date by updating to the latest version contact the ditto team through docid\ n4civtbqal1smgjno8am8