Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
长笛手/飞镖如何将索引从Listview.builder传递到项目小部件_Listview_Flutter_Dart - Fatal编程技术网

长笛手/飞镖如何将索引从Listview.builder传递到项目小部件

长笛手/飞镖如何将索引从Listview.builder传递到项目小部件,listview,flutter,dart,Listview,Flutter,Dart,我想在listview.builder中传递listview项目的索引,以便在项目小部件中从中删除这些项目 在类时间线中 我在Streambuilder中嵌入了Listview.builder void removePost(index) { setState(() { posts.remove(index); }); } @override Widget build(BuildContext context) { SizeConfig().init

我想在listview.builder中传递listview项目的索引,以便在项目小部件中从中删除这些项目

在类
时间线中
我在Streambuilder中嵌入了Listview.builder

void removePost(index) {
    setState(() {
      posts.remove(index);
    });
  }

@override
  Widget build(BuildContext context) {
    SizeConfig().init(context);
    return Scaffold(
        body: StreamBuilder<QuerySnapshot>(
          stream: postRef.orderBy('timestamp', descending: false).snapshots(),
          builder: (context, snapshot) {
            if (!snapshot.hasData) {
              return circularProgress();
            }
            List<Post> posts = snapshot.data.documents
                .map((doc) => Post.fromDocument(doc))
                .toList();
            if (posts.isEmpty) {
              return Text("No Posts");
            }
            return ListView.builder(
              itemCount: posts.length,
              itemBuilder: (context, index) {
                final item = posts[index];
                return Post/Item( //??
                  ????? //What comes here? How to pass the index?
                );
              },
            );
          },
        ));
  }
非常感谢您的帮助。

@Juju,您试过了吗

return ListView.builder(
              itemCount: posts.length,
              itemBuilder: (context, index) {
                final item = posts[index];
                return Post(index: index, title: 'Test', imageUrl: 'https://www.google.com',);
              },
            );
测试:

类MyApp扩展StatefulWidget{
@凌驾
MyAppState createState()=>MyAppState();
}
类MyAppState扩展了状态{
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
appBar:appBar(
标题:文本(“测试”),
),
主体:容器(
子项:ListView.builder(
物品计数:3,
itemBuilder:(上下文,索引){
//最后一项=员额[索引];
返回帖子(索引:索引,标题:'Test',图像URL:'https://www.google.com',);
},
),
)
)
);
}
}
类Post扩展StatefulWidget{
最终整数指数;
最后的字符串标题;
最终字符串imageUrl;
最终功能(Post)移除Post;
const Post({Key,this.index,this.title,this.imageUrl,this.removePost})
:super(key:key);
//工厂Post.fromDocument(文档快照文档){
//回程站(
//标题:文件[“标题”],
//imageUrl:doc['imageUrl'],
//    );
//  }
@凌驾
_PostState createState();
}
类_PostState扩展了状态{
@凌驾
小部件构建(构建上下文){
返回容器(
身高:150,
子:列(
儿童:[
Text(widget.title+“”+widget.index.toString()),//此处测试通过了索引
文本(widget.imageUrl),
图标按钮(
onPressed:()=>widget.removePost(this.widget),
图标:图标(Icons.delete),
)
],
),
);
}
}
截图:

由于不知道应该生成哪个项,因此引发错误。我想我需要在return语句中实现`` final item```所以你根本看不到你的列表吗?另外,您的
帖子
没有包含
,因此除非您更改
帖子
,否则您无法通过。请查看我编辑的代码,我运行了一个测试,我的答案似乎很好。是的,这很有效!当你按下图标时,它实际上是在删除项目吗?我正在传递
返回帖子(deletePost:deletePost,index:index,title:'Test',imageUrl:'https://www.google.com',);
并调用
IconButton(按下:()=>widget.removePost(widget.index),icon:icon(Icons.delete),
然后什么也不发生:(太好了。请将它标记为帮助其他人回答类似问题的答案。它不会在我这边删除,因为我没有你的帖子,但如果你通过了,它应该对你很好。)。
return ListView.builder(
              itemCount: posts.length,
              itemBuilder: (context, index) {
                final item = posts[index];
                return Post(index: index, title: 'Test', imageUrl: 'https://www.google.com',);
              },
            );
class MyApp extends StatefulWidget {
  @override
  MyAppState createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            appBar: AppBar(
              title: Text('Test'),
            ),
            body: Container(
                child: ListView.builder(
                  itemCount: 3,
                  itemBuilder: (context, index) {
                    //final item = posts[index];
                    return Post(index: index, title: 'Test', imageUrl: 'https://www.google.com',);
                  },
                ),
            )
        )
    );
  }
}

class Post extends StatefulWidget {
  final int index;
  final String title;
  final String imageUrl;
  final Function(Post) removePost;

  const Post({Key key, this.index, this.title, this.imageUrl, this.removePost})
      : super(key: key);

//  factory Post.fromDocument(DocumentSnapshot doc) {
//    return Post(
//      title: doc['title'],
//      imageUrl: doc['imageUrl'],
//    );
//  }

  @override
  _PostState createState() => _PostState();
}

class _PostState extends State<Post> {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 150,
      child: Column(
        children: <Widget>[
          Text(widget.title + ' ' + widget.index.toString()),// Testing passed index here
          Text(widget.imageUrl),
          IconButton(
            onPressed: () => widget.removePost(this.widget),
            icon: Icon(Icons.delete),
          )
        ],
      ),
    );
  }
}