1

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

4

2 回答 2

1

看看这一行:

chain((link)=>completer.complete(linkList));

Future.chain需要一个返回 a 的转换函数,Future但您向它传递了一个返回void( null) 的函数。NoSuchMethodError当尝试将错误转发到 this 时抛出异常null

我认为你需要then这里而不是chain.

于 2013-01-15T16:06:41.577 回答
0

我不确定我是否有所有信息来回答你的问题(我希望你能提供所有代码,以便我可以在本地重建应用程序并尝试调试它),但我会尽力而为。

我看到一个明显的问题:getAllLinks()返回一个Future<List>. 然而,当你调用它时

Future<bool> futureResult=new Link().getAllLinks();

你似乎认为你会从中受益bool。显然这段代码:

futureResult.then((result){
  print('final!!! ${result.length}');
});

result在不可迭代的上下文中没有什么意义。很明显,我会解决这个问题。

您是否在检查模式下工作?你在使用 Dart 编辑器吗?它会在你的代码中给你警告吗?

于 2013-01-11T20:37:25.163 回答