我已经使用 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 库上,但我不确定。有人可以帮助我吗?