如何从某些Dart代码解决奇怪的行为

时间:2019-05-21 17:43:19

标签: dart flutter

我在Firebase Realtime数据库的接口中有一些dart代码,我需要处理为数据库中特定路径设置流并返回所有条目或使用一对可选过滤器参数以仅获取所需条目的问题

该方法最初仅用于所有条目要求,并且可以正常工作:

class _DatabaseStream<T> {
  _DatabaseStream(
      {String apiPath,
      DatabaseNodeParser<T> parser,
      String key = '',
      String filter = ''}) {
    AppDatabase _database = AppDatabase.db;

    FirebaseDatabase _firebaseDatabase = _database.firebaseDatabaseInstance;
    DatabaseReference _databaseReference =
        _firebaseDatabase.reference().child(apiPath);
    var eventStream = _databaseReference.onValue;
    stream = eventStream.map((event) => parser.parse(event));
  }

  Stream<T> stream;
}

但是,当我编辑代码以处理可选的过滤器参数时,我从dart中获得了异常。这是更新的代码:

class _DatabaseStream<T> {
  _DatabaseStream(
      {String apiPath,
      DatabaseNodeParser<T> parser,
      String key = '',
      String filter = ''}) {
    AppDatabase _database = AppDatabase.db;

    FirebaseDatabase _firebaseDatabase = _database.firebaseDatabaseInstance;
    DatabaseReference _databaseReference =
        _firebaseDatabase.reference().child(apiPath);
    var eventStream;
    if (filter == '') {
      eventStream = _databaseReference.onValue;
      stream = eventStream.map((event) => parser.parse(event));
    } else {
      // we have a filter
      Query query = _databaseReference.orderByChild(key).equalTo(filter);
      eventStream = query.onValue;
      stream = eventStream.map((event) => parser.parse(event));
    }
  }

  Stream<T> stream;
}

报告的异常是:

I/flutter (32750): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (32750): The following assertion was thrown building EggsPage(dirty, state: _EggsPageState#93899):
I/flutter (32750): type '_MapStream<Event, dynamic>' is not a subtype of type 'Stream<List<Egg>>'
I/flutter (32750):
I/flutter (32750): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter (32750): more information in this error message to help you determine and fix the underlying cause.
I/flutter (32750): In either case, please report this assertion by filing a bug on GitHub:
I/flutter (32750):   https://github.com/flutter/flutter/issues/new?template=BUG.md

这真的是我需要报告的错误,还是我对该方法的更新有错误?

0 个答案:

没有答案