Design Converter
Education
Software Development Executive - II
Last updated on Sep 15, 2023
Last updated on Aug 16, 2023
Flutter is a software development kit known for building stunning UI with native performance. But even an application with mesmerizing looks needs a robust back-end to store and manipulate data. Here is where
Realm is a mobile database running directly on phones, tablets, wearables etc. It is designed to be a replacement for SQLite & Core Data, allowing you to work with real, live objects.
Not just that, combined with Flutter, it introduces an appealing data persistence feature set in our apps. Let's understand more about its prominence in
Flutter Realm interfaces with the Realm binary database, providing us with an efficient way to create, read, update, and delete records with minimal setup.
One of the remarkable aspects of Realm is the comprehensive package of features it offers.
It's the first database designed from the ground up to run directly inside phones, tablets, and wearables. It means you don't have to use server-side or cloud databases for storing your app data.
Despite offering a robust feature set, Flutter Realm has a simple and intuitive API that needs less code for setting up databases. Additionally, it supports the latest Dart and Flutter versions and is built with sound null safety. This ensures your Flutter Realm DB stays up to date with the current development ecosystem.
In your Dart file app.dart, you need to import the Realm package and declare a part file.
1 import 'package:realm/realm.dart'; // import realm package 2 3 part 'app.g.dart'; // declare a part file. 4
For using Realm in your Flutter application, you need to define a data model class. We would define a simple _Car class as follows:
1 @RealmModel() // define a data model class named `_Car`. 2 class _Car { 3 late String make; 4 5 late String model; 6 7 int? kilometers = 500; 8 } 9
A real-life example enables us to understand Flutter Realm at its best. Generating RealmObject classes from our data model class is straightforward, all you need to do is run the following dart command.
1 dart run realm generate 2
Following the successful execution of this command, a RealmObject class Car will be generated from the data model class _Car. The class Car can now be used with Realm objects. We can create a configuration object and open a realm with an instance of our class.
1 var config = Configuration.local([Car.schema]); 2 var realm = Realm(config); 3 4 var car = Car("Tesla", "Model Y", kilometers: 5); 5 realm.write(() { 6 realm.add(car); 7 }); 8
This Flutter Realm example allows us to add a new Car instance to our Realm database within a write transaction. As simple as that!
Setting up the Flutter Realm SDK can be done using these simple steps. It starts with the [h4] Environment Setup. It's worthwhile mentioning that Realm Flutter supports platforms including iOS, Android, Windows, MacOS, and Linux. You will need Flutter 3.0.3 or newer, Cocoapods v1.11 or newer, and CMake 3.21 or newer for a smooth setup process.
Adding Realm to your Flutter application is easy. After you have made sure the environment is ready, follow these steps:
1 // Add realm package to a Flutter application 2 flutter pub add realm 3 // To install the required native binaries 4 dart run realm install 5
The Realm Dart Standalone SDK offers the same level of robust and efficient database management as the Flutter Realm SDK. The setup remains quite similar. The [h4] Environment Setup for Realm Dart requires Dart SDK 2.17.5 or newer. It supports platforms like Windows, Mac, and Linux.
You can just include the realm_dart package in your Canvas Dart application:
1 // Add realm_dart package to a Dart application 2 dart pub add realm_dart 3 // To install the required native binaries, run the following command 4 dart run realm_dart install 5
Syncing data across devices and backend in real-time is a great advantage that Flutter Realm provides. Thanks to MongoDB Realm Flutter, the setup of this feature involves some simple, easy-to-follow steps.
The process involves three main parts:
You do this by creating an account on cloud.mongodb.com and following the instructions for new account registration. After registering, you will need to create a new App and configure the apt authentication provider type.
You will need to initialize the App Services App client and authenticate a user. Then, you will set up a sync subscription and start adding data from your Flutter app to Realm DB. Only the data matching the query in the subscription is synced to the server and the local device realm file.
1 String appId = "<Atlas App ID>"; 2 final appConfig = AppConfiguration(appId); 3 final app = App(appConfig); 4 final user = await app.logIn(Credentials.anonymous()); 5 6 // Open a synced realm 7 final config = Configuration.flexibleSync(user, [Task.schema]); 8 final realm = Realm(config); 9
After you've gone through these steps, congratulations, you've built a fully functioning Flutter application leveraging the power of Realm!
We entered this blog with a curiosity about Flutter Realm and now we have a comprehensive understanding of it. We’ve taken a deep dive into Flutter Realm DB , exploring its features, understanding how to start using it, looking at some practical examples, and even syncing data with Realm Flutter using device sync.
This exciting journey taught us how Realm's mobile-first, simplicity, and modernity can equate to a robust data management solution for our Flutter applications. Furthermore, the extensible documentation can be a long-lasting companion in our Flutter Realm journey.
By embracing Flutter Realm, we are not just choosing a mobile database but are stepping towards effective and efficient mobile application development. This decision can indeed steer our Flutter projects to new heights.
We hope you enjoyed this in-depth guide and it assists you in your Flutter Realm adventures. Now, it's your turn to turn these guides into reality and unlock the full potential of Flutter Realm!
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.