HomeLearnArticleIntroducing the Realm Flutter SDK

Introducing the Realm Flutter SDK

Updated: Apr 08, 2022 |

Published: Jan 31, 2022

  • Realm
  • Mobile
  • C
  • ...

By Ian Ward

Rate this article

Today, we are pleased to announce the next installment of the Realm Flutter SDK – now with support for Windows, macOS, iOS, and Android. This release gives you the ability to use Realm in any of your Flutter or Dart projects regardless of the version.

Realm is a simple super-fast, object-oriented database for mobile applications that does not require an ORM layer or any glue code to work with your data layer. With Realm, working with your data is as simple as interacting with objects from your data model. Any updates to the underlying data store will automatically update your objects as soon as the state on disk has changed, enabling you to automatically refresh the view via StatefulWidgets and Streams.

#Introduction

Flutter has been a boon to developers across the globe, a framework designed for all platforms: iOS, Android, server and desktop. It enables a developer to write code once and deploy to multiple platforms. Optimizing for performance across multiple platforms that use different rendering engines and creating a single hot reload that work across all platforms is not an easy feat, but the Flutter and Dart teams have done an amazing job. It’s not surprising therefore that Flutter support is our top request on Github.

Realm’s Core Database is platform independent meaning it is easily transferable to another environment which has enabled us to build SDKs for the most popular mobile development frameworks: iOS with Swift, Android with Kotlin, React Native, Xamarin, Unity, and now Flutter.

Our initial version of the Flutter SDK was tied to a custom-built Flutter engine. It was version-specific and shipped as a means to gather feedback from the community on our Realm APIs. With this latest version, we worked closely with the Flutter and Dart team to integrate with the Dart FFI APIs. Now, developers can use Realm with any version of their Dart or Flutter projects. More importantly though, this official integration will form the underpinning of all our future releases and includes full support from Dart’s null safety functionality. Moving forward, we will continue to closely partner with the Flutter and Dart team to follow best practices and ensure version compatibility.

#Why Realm

All of Realm’s SDKs are built on three core concepts:

  • An object database that infers the schema from the developers’ class structure – making working with objects as easy as interacting with their data layer. No conversion code necessary
  • Live objects so the developer has a simple way to update their UI – integrated with StatefulWidgets and Streams
  • A columnar store so that query results return in lightning speed and directly integrate with an idiomatic query language the developer prefers

Realm is a database designed for mobile applications as a replacement for SQLite. It was written from the ground up in C++, so it is not a wrapper around SQLite or any other relational datastore. Designed with the mobile environment in mind, it is lightweight and optimizes for constraints like compute, memory, bandwidth, and battery that do not exist on the server side. Realm uses lazy loading and memory mapping with each object reference pointing directly to the location on disk where the state is stored. This exponentially increases lookup and query speed as it eliminates the loading of state pages of disk space into memory to perform calculations. It also reduces the amount of memory pressure on the device while working with the data layer.

#Realm for Flutter Developers

Since Realm is an object database, your schema is defined in the same way you define your object classes. Additionally, Realm delivers a simple and intuitive string-based query system that will feel natural to Flutter developers. No more context switching to SQL to instantiate your schema or looking behind the curtain when an ORM fails to translate your calls into SQL. And because Realm object’s are memory-mapped, a developer can bind an object or query directly to the UI. As soon as changes are made to the state store, they are immediately reflected in the UI. No need to write complex logic to continually recheck whether a state change affects objects or queries bound to the UI and therefore refresh the UI. Realm updates the UI for you.

1// Import the Realm package and set your app file name
2import 'package:realm_dart/realm.dart';
3
4part 'test.g.dart'; // if this is test.dart
5
6// Set your schema
7@RealmModel()
8class _Task {
9 late String name;
10 late String owner;
11 late String status;
12}
13
14void main(List<String> arguments) {
15 // Open a realm database instance. Be sure to run the Realm generator to generate your schema
16 var config = Configuration([Task.schema]);
17 var realm = Realm(config);
18
19 // Create an instance of your Tasks object and persist it to disk
20 var task = Task("Ship Flutter", "Lubo", "InProgress");
21 realm.write(() {
22 realm.add(task);
23 });
24
25 // Use a string to based query language to query the data
26 var myTasks = realm.all<Task>().query("status == 'InProgress'");
27
28 var newTask = Task("Write Blog", "Ian", "InProgress");
29 realm.write(() {
30 realm.add(newTask);
31 });
32
33 // Queries are kept live and auto-updating - the length here is now 2
34 myTasks.length;
35}

#Looking Ahead

The Realm Flutter SDK is free, open source and available for you to try out today. While this release is still in Alpha, our development team has done a lot of the heavy lifting to set a solid foundation – with a goal of moving rapidly into Beta and GA later this year. We will look to bring new notification APIs, a migration API, solidify our query system, helper functions for Streams integration, and of course Realm Sync to automatically replicate data to MongoDB Atlas.

Give it a try today and let us know what you think! Check out our samples, read our docs, and follow our repo.

Rate this article
MongoDB logo
© 2021 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.