Invalid scheme in uri mongo_dart in flutter

Viewed 412

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?

1 Answers

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');
}
Related