Flutter 在颤振中使用SliverList的SliverChildBuilderDelegate时,索引来自何处

Flutter 在颤振中使用SliverList的SliverChildBuilderDelegate时,索引来自何处,flutter,Flutter,现在我在颤振中使用SliverList,在组件中我使用SliverList,如下所示: @override Widget buildResults(BuildContext context) { var channelRequest = new ChannelRequest(); channelRequest.name = query; channelRequest.pageNum = 1; channelRequest.pageSize = 10;

现在我在颤振中使用SliverList,在组件中我使用SliverList,如下所示:

@override
  Widget buildResults(BuildContext context) {
    var channelRequest = new ChannelRequest();
    channelRequest.name = query;
    channelRequest.pageNum = 1;
    channelRequest.pageSize = 10;

    return FutureBuilder(
        future: ChannelAction.searchChannel(channelRequest),
        builder: (context, AsyncSnapshot snapshot) {
          if (snapshot.hasData) {
            List<Channel> post = snapshot.data;
            if (post != null) {
              return CustomScrollView(
                slivers: [
                  SliverList(delegate: SliverChildBuilderDelegate((context, index) {
                    return Slidable(
                      actionPane: SlidableScrollActionPane(),
                      child: Container(width: 0.0, height: 0.0),
                    );
                  }))
                ],
              );
            }
          }
          return Center(child: CircularProgressIndicator());
        });
  }
@覆盖
小部件构建结果(构建上下文){
var channelRequest=新channelRequest();
channelRequest.name=查询;
channelRequest.pageNum=1;
channelRequest.pageSize=10;
回归未来建设者(
未来:ChannelAction.searchChannel(channelRequest),
生成器:(上下文,异步快照){
if(snapshot.hasData){
List post=snapshot.data;
如果(post!=null){
返回自定义滚动视图(
条子:[
SliverList(委托:SliverChildBuilderDelegate((上下文,索引){
返回可滑动(
actionPane:SlideableScrollActionPane(),
子项:容器(宽度:0.0,高度:0.0),
);
}))
],
);
}
}
返回中心(子项:CircularProgressIndicator());
});
}

现在我不知道索引是什么时候来的,也不知道如何将列表传递到
SliverList
,似乎索引是无限的。索引从哪里来?

您可以将
childCount
参数传递给
SliverChildBuilderDelegate
。 然后,
SliverChildBuilderDelegate
将调用传递给它的函数,其索引为0到
childCount
,您可以使用本地范围或类中的变量访问数据

例如:

class ExampleWidget extends StatelessWidget {
  const ExampleWidget({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final data = ["a", "b", "c"];

    return CustomScrollView(
      slivers: [
        SliverList(
          delegate: SliverChildBuilderDelegate(
              (context, index) => Text("$index: ${data[index]}"),
              childCount: data.length), // <-- child count set here
        )
      ],
    );
  }
}
class ExampleWidget扩展了无状态Widget{
const-ExampleWidget({Key}):super(Key:Key);
@凌驾
小部件构建(构建上下文){
最终数据=[“a”、“b”、“c”];
返回自定义滚动视图(
条子:[
银表(
代表:SliverChildBuilderDelegate(
(上下文,索引)=>Text(“$index:${data[index]}”),

childCount:data.length),//您可以将
childCount
参数传递给
SliverChildBuilderDelegate
。 然后,
SliverChildBuilderDelegate
将调用传递给它的函数,其索引为0到
childCount
,您可以使用本地范围或类中的变量访问数据

例如:

class ExampleWidget extends StatelessWidget {
  const ExampleWidget({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final data = ["a", "b", "c"];

    return CustomScrollView(
      slivers: [
        SliverList(
          delegate: SliverChildBuilderDelegate(
              (context, index) => Text("$index: ${data[index]}"),
              childCount: data.length), // <-- child count set here
        )
      ],
    );
  }
}
class ExampleWidget扩展了无状态Widget{
const-ExampleWidget({Key}):super(Key:Key);
@凌驾
小部件构建(构建上下文){
最终数据=[“a”、“b”、“c”];
返回自定义滚动视图(
条子:[
银表(
代表:SliverChildBuilderDelegate(
(上下文,索引)=>Text(“$index:${data[index]}”),
childCount:data.length),//
不知道索引是从什么时候来的,如何将列表传递到SliverList中,似乎索引是无限的。索引是从哪里来的

简单地说,你是对的。如果你没有通过
SliverChildBuilderDelegate
childCount
,那么你将拥有无限的可滚动列表。因此
索引是无限项中的当前项

不知道索引是从什么时候来的,如何将列表传递到SliverList中,似乎索引是无限的。索引是从哪里来的

简单地说,你是对的。如果你没有通过
SliverChildBuilderDelegate
childCount
,那么你将拥有无限的可滚动列表。因此
索引是无限项中的当前项