Invalid scheme in uri mongo_dart in flutter

1.2k views Asked by At

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:[email protected]/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

There are 1 answers

0
user3187759 On

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:[email protected]/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:[email protected]/databaseName');
}