AsyncSnapshot 拒绝类型注释

AsyncSnapshot rejecting Type Annotation

我有一个 StreamBuilder 从我的 bloc 组件中获取数据。

但是它一直拒绝我的类型注释 AsyncSnapshot<List<int>> snapshot 并且只接受 dynamic 作为类型 AsyncSnapshot<List<dynamic>> snapshot。然而在我看过的例子中,他们确实有类型注释,没有任何抱怨。

这是我创建的流。

Widget buildList(StoriesBloc bloc) {
return StreamBuilder(
  stream: bloc.topIds,
  builder: (BuildContext context, AsyncSnapshot<List<int>> snapshot) {
    if (!snapshot.hasData) {
      return Text("Still Waiting for Ids to fetch");
    }

    return ListView.builder(
      itemCount: snapshot.data.length,
      itemBuilder: (context, int index) {
        return Text('${snapshot.data[index]}');
      },
    );
  },
);

}

这是生成的 VSCode 错误。

我做错了什么?

原来我的 bloc.topIds 结果类型不是 List<int> 类型。

Observable<List> get topIds => _topIds.stream;

所以我简单地更改了它以满足所需的类型。

Observable<List<int>> get topIds => _topIds.stream;

这就解决了问题。希望对其他人有帮助。