我使用Mongo Dart和Future开发了一个功能。当我使用Dart SDK 0.2.9执行它时,它毫无例外地工作。
import 'model.dart';
import 'dart:json';
import 'package:mongo_dart/mongo_dart.dart';
class LinkDAO {
DbCollection _links;
Future<List> getAllLinks(){
var completer = new Completer<List>();
Db db = new Db("mongodb://127.0.0.1:27017/dart-link");
List linkList = new List();
_links = db.collection("links");
db.open().chain((c){
return _links.find({}).each(
(link){
print("[${link['link']}]:[${link['readed']}]:[${link['title']}]");
linkList.add(new Link(link['link'],link['title'],link['readed']));
print('lenght ${linkList.length}');
}).chain((link)=>completer.complete(linkList));
}).then((dummy){
db.close();
});
return completer.future;
}
}
void main(){
Future<List> futureResult=new LinkDAO().getAllLinks();
futureResult.then((result){
print("final!!! -> ${result.length}");
});
}
这是链接类
library models;
class Link {
String title;
String link;
bool readed;
Link(this.title,this.link,this.readed);
}
更新到版本0.2.10之后,同样的函数会生成此异常:
[assas]:[true]:[sasas]
lenght 1
[assas]:[true]:[sasas]
lenght 2
final!!! -> 2
Unhandled exception:
NoSuchMethodError : method not found: 'handleException'
Receiver: null
Arguments: [Closure: _FutureImpl@0x36924d72<T>(dynamic, dynamic) => dynamic]
#0 Object._noSuchMethod (dart:core-patch:1360:3)
#1 Object.noSuchMethod (dart:core-patch:1361:25)
#2 _FutureImpl._forwardException (bootstrap:935:27)
#3 _FutureImpl._forward (bootstrap:931:22)
#4 _FutureImpl.chain.<anonymous closure> (bootstrap:903:15)
#5 _FutureImpl._complete (bootstrap:840:19)
#6 _FutureImpl._complete (bootstrap:848:5)
#7 _FutureImpl._setValue (bootstrap:862:14)
#8 _CompleterImpl.complete (bootstrap:949:26)
#9 Cursor._nextEach.<anonymous closure> (package:mongo_dart/src/database/cursor.dart:112:30)
#10 _FutureImpl.then (bootstrap:792:16)
#11 Cursor._nextEach (package:mongo_dart/src/database/cursor.dart:109:22)
#12 Cursor._nextEach.<anonymous closure> (package:mongo_dart/src/database/cursor.dart:115:18)
#13 _FutureImpl.then (bootstrap:792:16)
#14 Cursor._nextEach (package:mongo_dart/src/database/cursor.dart:109:22)
#15 Cursor._nextEach.<anonymous closure> (package:mongo_dart/src/database/cursor.dart:115:18)
#16 _FutureImpl._complete (bootstrap:840:19)
#17 _FutureImpl._complete (bootstrap:848:5)
#18 _FutureImpl._setValue (bootstrap:862:14)
#19 _CompleterImpl.complete (bootstrap:949:26)
#20 Cursor.nextObject.<anonymous closure> (package:mongo_dart/src/database/cursor.dart:74:28)
#21 _FutureImpl._complete (bootstrap:840:19)
#22 _FutureImpl._complete (bootstrap:848:5)
#23 _FutureImpl._setValue (bootstrap:862:14)
#24 _CompleterImpl.complete (bootstrap:949:26)
#25 Connection._receiveData._receiveData (package:mongo_dart/src/database/connection.dart:84:27)
#26 _SocketBase._multiplex (dart:io-patch:410:26)
#27 _SocketBase._sendToEventHandler.<anonymous closure> (dart:io-patch:512:20)
#28 _ReceivePortImpl._handleMessage (dart:isolate-patch:40:92)
我认为问题出在Mogo Dart图书馆,但我不确定。有人可以帮帮我吗?
答案 0 :(得分:1)
看看这一行:
chain((link)=>completer.complete(linkList));
Future.chain
需要一个返回Future
的转换函数,但是你传递一个返回void
(null
)的函数。 NoSuchMethodError时,会抛出null
异常。
我认为您需要then
而不是chain
。
答案 1 :(得分:0)
我不确定我是否有所有信息可以回答您的问题(我希望您提供所有您的代码,以便我可以在本地重建应用并尝试调试它),但我和# 39;我会尽我所能。
我发现一个明显的问题:getAllLinks()
会返回Future<List>
。然而,当你称之为
Future<bool> futureResult=new Link().getAllLinks();
你似乎认为你会得到一个bool
。显然这段代码:
futureResult.then((result){
print('final!!! ${result.length}');
});
在result
不是可迭代的上下文中没有意义。很清楚,我会解决这个问题。
您是否在检查模式下工作?你在使用Dart编辑器,它是否在你的代码中给你警告?