3

I tried accessing a MongoDB database using the mongo_dart in flutter and it gives the following error:

   "MongoDart Error: Invalid scheme in uri:" followed by the uri which is of the following form: 
    mongodb+srv://user:password@mongodb.net/databaseName?options. 

The connection string otherwise works when seeing the database on mongodb compass. Any ideas what might be wrong/ different ways I can make the connection?

Akif
  • 7,098
  • 7
  • 27
  • 53
nimeni
  • 43
  • 2

1 Answers1

2

I faced the same issue when using the Db constructor directly. i.e;

import 'package:mongo_dart/mongo_dart.dart';

class MongoDbProvider {
  static Db db;
  MongoDbProvider.db = await Db('mongodb+srv://user:password@mongodb.net/databaseName');
}

However, when using the Db.create() function (which returns a Db instance), I was able to get it to work with no issues.

import 'package:mongo_dart/mongo_dart.dart';

class MongoDbProvider {
  static Db db;
  MongoDbProvider.db = await Db.create('mongodb+srv://user:password@mongodb.net/databaseName');
}
user3187759
  • 187
  • 1
  • 16