BLOGAtlas Vector Search voted most loved vector database in 2024 Retool State of AI report — Read more >>

Flutter and Compatibility with MongoDB

Flutter is a framework or toolkit developed by Google, used to build cross-platform applications using customizable, pre-built widgets. Realm is an embedded database that is designed with the unpredictable nature of mobile devices in mind. Realm runs on the client device, providing data resilience in case of bad network connectivity. The database is offline-first with the option to sync data between the device, the back end, and all users provided by Atlas Device Sync. You can use Realm’s open source SDKs to connect to Atlas App Services, set up authentication and user roles, call serverless functions, and more.

Flutter and compatibility with MongoDB

To use Flutter apps with MongoDB Realm, you can use the MongoDB Realm SDK for Flutter.

You can also use the mongo_dart package, which is a community-supported, server-side library written in the Dart programming language.

Before exploring all these, let’s learn a little more about Flutter.

What is Flutter?

Flutter has pre-built widgets that help you create powerful UI for mobile apps. Flutter applications are written in the Dart programming language and can run on multiple different platforms — mobile, web, desktop, and even embedded. Using Flutter, you can add everything — from row and column layouts, padding, aspect ratio, and screen sizing to animations — and it works seamlessly across devices.

The core of Flutter is the Flutter engine, written in C++ programming language. The engine is responsible for layouting and rendering widgets on the screen. When developing a Flutter app, you’d normally build user interfaces through the Flutter framework, which is an abstraction on top of the engine. The framework is written in Dart. Flutter follows a reactive approach to building UI. The developer provides the application and interface mapping, and the framework updates the interface as the application state changes at runtime.

How to connect Flutter with MongoDB


MongoDB and Flutter How to connect to MongoDB from Flutter apps


There are two ways to connect Flutter to MongoDB:

  • Using the Realm Flutter SDK
  • Using the mongo_dart community driver

Connecting Flutter and MongoDB Realm using Realm Flutter SDK

You can add the Realm Flutter SDK to your project using the flutter pub add realm command or by adding it as a dependency in your configuration file:

dependencies:
  realm: <latest_version>

Import the package and use it in your code:

import 'package:realm/realm.dart';

You can then create Realm objects, sync them with your data models, perform CRUD operations, sync Realm data with Atlas, create functions, access them as APIs, and much more.

To get started with the Realm Flutter SDK, follow the guided tutorial.

Connecting to Atlas with the community driver for Dart

If you have a MongoDB Atlas cluster, simply click on “Connect.” MongoDB will give you the URL that you need to use to connect to the particular cluster and database from your Flutter app. The URL will be similar to:

mongodb+srv://<username>:<password>@<cluster>.mongodb.net/?retryWrites=true&w=majority

If you do not have a cluster yet, it takes just a few simple steps to create one from the MongoDB Atlas page. You can create a cluster and add a sample dataset or your own database(s).

Now that you have the cluster in place and the required databases in your cluster, let’s move to the Flutter app.

To connect to Atlas database, you need the mongo_dart package. Install it using the $flutter pub add mongo_dart command or by adding the dependency in your project configuration file as:

'dependencies: mongo_dart: ^<version>'

Next, import the package in your Flutter code, add the connection URL obtained from Atlas page, and get the collection:

import 'package:mongo_dart/mongo_dart.dart';
Db.create(CONNECTION_URL) 
db.collection("collection_name")

You can then perform all the CRUD operations.

Conclusion

The article explains briefly how easily we can connect Flutter apps to MongoDB. Realm is an embedded database built from the ground up for mobile and devices with limited capabilities. The compatibility of MongoDB and Flutter lets you quickly build reliable and secure cross-platform and mobile applications.

FAQ

Can I use MongoDB with Flutter?

Yes, you can use MongoDB and Flutter in the following ways:

  • Using the mongo_dart package to connect to MongoDB Atlas.
  • Adding MongoDB Realm via the MongoDB Flutter SDK in your Flutter app.

Which database is best for Flutter?

Flutter is compatible with both SQL and NoSQL databases. For example, SQFlite is a SQLite database specifically designed for Flutter. Similarly, MongoDB Realm is a serverless platform designed with mobile apps in mind and provides seamless integration between Flutter objects and database objects.

What is Realm MongoDB?

MongoDB Realm is a mobile-first database that lets you build real-time applications and provides access to Atlas App Services like serverless JavaScript Functions, Authorization, Triggers, and much more through open-source SDKs.

What database does Flutter use?

Flutter is a toolkit that provides pre-built widgets to build the front end of a mobile application. It does not depend on any database. However, you can choose any compatible database — SQL or NoSQL (like MongoDB) — to connect Flutter applications, depending on your requirements.

Is Flutter a front end or back end?

Flutter is a UI toolkit that provides the front end of a mobile application.