在Flutter中MongoDB Atlas的create()函数中出现错误。

huangapple go评论68阅读模式
英文:

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

问题

我正在使用Atlas服务来访问我的Flutter项目中的数据库。调试构建正常工作,但在发布构建中,用于实例化数据库对象的create()方法出现了某种错误。我已经在AndroidManifest.xml中添加了INTERNET权限,但错误仍然存在。

这是在运行发布构建时终端的日志:

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):

mongo_db.dart的第21行是:

_db = await Db.create(mongoURL);

而main.dart的第13行是调用connect方法:

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

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

答案1

得分: 0

请确保您已经在您的应用程序的 android/app/src/main/AndroidManifest.xml 文件中添加了以下内容:

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

https://i.stack.imgur.com/aVDa3.png

在之前的Flutter版本中,默认情况下会有这行代码。似乎它们在最近的更新中已被移除。

英文:

Make sure you have added

&lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt; 

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

https://i.stack.imgur.com/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.

huangapple
  • 本文由 发表于 2023年6月26日 01:23:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定