Search code examples
mongodbfluttermongodb-atlas

Getting error in create() function of MongoDB Atlas in Flutter


I am using the atlas service to access a database from my flutter project. The debug build is working fine but in the release build the create() method to instantiate the database object is giving somekind of error. I have added the INTERNET permission in the AndroidManifest.xml but the error is still there.

This is the log of the terminal in running release build

E/flutter (14697): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Failed host lookup: ‘dns.google.com’
E/flutter (14697): #0 IOClient.send (package:http/src/io_client.dart:94)
E/flutter (14697):
E/flutter (14697): #1 BaseClient._sendUnstreamed (package:http/src/base_client.dart:93)
E/flutter (14697):
E/flutter (14697): #2 HttpUtils._get (package:basic_utils/src/HttpUtils.dart:24)
E/flutter (14697):
E/flutter (14697): #3 HttpUtils.getForJson (package:basic_utils/src/HttpUtils.dart:49)
E/flutter (14697):
E/flutter (14697): #4 DnsUtils.lookupRecord (package:basic_utils/src/DnsUtils.dart:33)
E/flutter (14697):
E/flutter (14697): #5 decodeDnsSeedlist (package:mongo_dart/src/database/utils/dns_lookup.dart:19)
E/flutter (14697):
E/flutter (14697): #6 Db.create (package:mongo_dart/src/database/db.dart:271)
E/flutter (14697):
E/flutter (14697): #7 MongoDb.connect (package:student_portal/services/mongo_db.dart:21)
E/flutter (14697):
E/flutter (14697): #8 main (package:student_portal/main.dart:13)
E/flutter (14697):
E/flutter (14697):

21th line of the mongo_db.dart is

_db = await Db.create(mongoURL);

and in 13th line of main I am calling the connect method

Future connect() async {
_db = await Db.create(mongoURL);
await _db!.open(secure: true);
}

Solution

  • Make sure you have added

    <uses-permission android:name="android.permission.INTERNET" /> 
    

    in your app's android/app/src/main/AndroidManifest.xml

    https://i.sstatic.net/aVDa3.png

    This line used to be there on default on previous version of Flutter. It seems that they have removed on the recent updates.