Back

[SOLVED] How to connect MongoDB with flutter

  • 1
  • Databases
  • Flutter
  • Storage
Pratham Dave
6 Apr, 2023, 05:29

I am trying to connect my mongodb to flutter and this is how my database.dart file looks

TypeScript
import 'package:mongo_dart/mongo_dart.dart';
import 'package:restro_app/database/constant.dart';
import 'package:restro_app/models/menu_model.dart';

class MongoDatabase {
  static var db, userCollection;

  //Connection to the database
  static connect() async {
    db = await Db.create(MONGO_CONN_URL);
    await db.open();

    userCollection = db.collection(DATA_COLLECTION);
  }

  // More code..
TL;DR
The OP was having trouble connecting MongoDB with Flutter. They were getting `Db(null, null)` and noticed that the `db` variable was not getting any values. They shared their code from `database.dart` where they were creating the connection. The solution to their problem was to use a public IP or a domain to connect to the MongoDB server. The `MONGO_CONN_URL` needed to be updated with the correct IP or domain. The thread was closed with a recommendation to ask general Flutter questions in a Flutter Discord server.
Pratham Dave
6 Apr, 2023, 05:29

But when I started to debug I realised that the variable db isn't getting any values

Pratham Dave
6 Apr, 2023, 05:31

it returns Db(null, null)

Pratham Dave
6 Apr, 2023, 05:32

const MONGO_CONN_URL = "mongodb+srv://127.0.0.1:27017/Restaurant";

Pratham Dave
6 Apr, 2023, 05:32

This is how my connection string looks

Pratham Dave
6 Apr, 2023, 05:32

Not sure what is going wrong

joeyouss
6 Apr, 2023, 15:51

Hi πŸ‘‹

joeyouss
6 Apr, 2023, 15:53

127.0.0.1 - is localhost?

joeyouss
6 Apr, 2023, 15:56

You need to use public IP or a domain to connect to their MongoDB server.

Drake
6 Apr, 2023, 16:17

FYI, since this is an Appwrite (https://appwrite.io/) discord server, for general flutter questions, you might find more help in a Flutter discord server (https://flutter.dev/community#community-grid).

Tessa
7 Apr, 2023, 21:10

[SOLVED] How to connect MongoDB with flutter

Tessa
7 Apr, 2023, 21:10

Closing this thread for now, if you have appwrite specific questions, please open a new thread πŸ™‚

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more