1

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

但是,它一直拒绝我的类型注释AsyncSnapshot<List<int>> snapshot,并且只接受 dynamic 作为 type 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 错误。 VS 代码错误

我可能做错了什么?

4

1 回答 1

0

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

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

所以我只是改变了它来满足所需的类型。

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

这解决了这个问题。希望它可以帮助别人。

于 2019-05-02T12:46:21.203 回答