颤振ListView,在按back时导致重复值

颤振ListView,在按back时导致重复值,listview,flutter,Listview,Flutter,我有两个类别类型过滤,以便获得帖子列表,然后传递到详细信息页面。我创建了一个新的列表来获得这个帖子 // I believed this is the reason of the error, when it back it loop again. list<Post> cat1 = []; list<Post> cat2 = []; ........ Widget _postList ( List<Post> posts ) { for ( v

我有两个类别类型过滤,以便获得帖子列表,然后传递到详细信息页面。我创建了一个新的列表来获得这个帖子

// I believed this is the reason of the error, when it back it loop again.
list<Post> cat1 = [];   
list<Post> cat2 = [];

........

Widget _postList ( List<Post> posts ) {
  for ( var item in posts) {
    if (widget.cat1postsids.contains(item.id)) {
      cat1.add(item);
  }}
  return Container(
    child: _fetchPostbyCat1( cat1 )
  );
}

Widget _fetchPostbyCat1 ( List<Post> posts ) {
  for ( var item in posts) {
    if (widget.cat2postsids.contains(item.id)) {
      cat2.add(item);
  }}
  return Container(
    child: _getPost( cat2 )
  );
}

Widget _getPost ( List<Post> posts ) {
  return ListView.builder .... 
}
class PostListView extends StatefulWidget {

   final List selectedIDS;
   final String name;
   PostListView(this.selectedIDS,  this.name);

   @override
   PostListViewState createState() => new PostListViewState();

}

class PostListViewState extends State<PostListView> {

   PostApi postapi;
   List <Post> getPostByCat1 = [];
   List <Post> getPostByCat2 = [];

   @override
   void initState() {
      super.initState();
      postapi = PostApi();
   }

   @override
   Widget build(BuildContext context) {

      return Scaffold(
      appBar: AppBar(
      title: Text('Post'),
      leading: new IconButton(
         icon: new Icon(Icons.arrow_back),
         onPressed: () { Navigator.pop(context, true); })),

      body: NestedScrollView(
        headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
        return [ SliverList(
           delegate: SliverChildListDelegate([
              Container(
                 child: Padding(
                    padding: EdgeInsets.only(left: 20.0, top: 30.0, right: 20.0, bottom: 30.0 ),
                    child: Column(
                    mainAxisSize: MainAxisSize.max,
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: <Widget>[
                       AutoSizeText( 'LATEST POST', maxLines: 2,
                       textAlign: TextAlign.center,
                       style: TextStyle (fontSize: 28, color: Colors.black, fontWeight: FontWeight.w700))
                    ])),
                    decoration: new BoxDecoration(
                        border: Border(
                          bottom: BorderSide(width: 1.0, color: Colors.grey ))))]))];},
        body: SafeArea(
            child: SizedBox(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Expanded(
                        child: ListView (
                            children: <Widget>[
                              Container(
                                  child: FutureBuilder(
                                    future: postapi.fetchAllPost(),
                                    builder: (BuildContext context, AsyncSnapshot snapshot) {

                                      if( snapshot.connectionState == ConnectionState.done ) {
                                        if( snapshot.hasError ) {
                                          return returnerror( snapshot.error.toString());
                                        }
                                      }
                                      if( snapshot.hasData) {
                                        return _postList( snapshot.data );
                                      }
                                      return Center ( child: CircularProgressIndicator());

                                    }))]))])))));
}

Widget _postList ( List<Post> posts ) {
   for ( var item in posts) {
     if (widget.name.contains(item.catname)) {
       getPostByCat1.add(item);
   }}
   return Container( child: _getPosts(getPostByCat1));
}

Widget _getPosts (List<Post> posts) {
  for(var item in getPostByCat1){
    if(widget.selectedIDS.contains(item.post_id))  {
      getPostByCat2.add(item);
    }
  } return Container(child: _listPosts(getPostByCat2));
}

Widget _listPosts (List<Post> posts) {
  return ListView.builder(
  physics: ClampingScrollPhysics(),
  shrinkWrap: true,
  itemCount: posts.length,
  itemBuilder: ( BuildContext context, int index ) {

  var imgUrl = ApiUtil.MAIN_API_URL + ApiUtil.IMAGE_PATH  + posts[index].image;

  String postID = posts[index].id;
  String postname = posts[index].name;

  return new Card (
     child: InkWell(
        onTap: () {
           Navigator.push(context, MaterialPageRoute(
              builder: (context) => PostDetails(postID, postname)));},
            child: Padding(
              padding: const EdgeInsets.all(10.0),
              child: new Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[

                    Expanded(
                      flex: 1,
                      child: Image.network(imgUrl),
                    ),

                    Expanded(
                      flex: 3,
                      child: Padding(
                          padding: EdgeInsets.all(10.0),
                          child: ListTile(
                            title: new Text(posts[index].name,
                              style: TextStyle(
                                  fontSize: 22,
                                  fontWeight: FontWeight.w700
                              ),
                            ),
                            subtitle: new Text(posts[index].subname,
                              style: TextStyle(
                                fontSize: 16,

  )))))])))); });}
 }
和“返回”按钮

onPressed: () {
  Navigator.pop(context, true);
  return Future.value(true);
}
实施它的最佳方式是什么。非常感谢。 下面是文章的列表视图

// I believed this is the reason of the error, when it back it loop again.
list<Post> cat1 = [];   
list<Post> cat2 = [];

........

Widget _postList ( List<Post> posts ) {
  for ( var item in posts) {
    if (widget.cat1postsids.contains(item.id)) {
      cat1.add(item);
  }}
  return Container(
    child: _fetchPostbyCat1( cat1 )
  );
}

Widget _fetchPostbyCat1 ( List<Post> posts ) {
  for ( var item in posts) {
    if (widget.cat2postsids.contains(item.id)) {
      cat2.add(item);
  }}
  return Container(
    child: _getPost( cat2 )
  );
}

Widget _getPost ( List<Post> posts ) {
  return ListView.builder .... 
}
class PostListView extends StatefulWidget {

   final List selectedIDS;
   final String name;
   PostListView(this.selectedIDS,  this.name);

   @override
   PostListViewState createState() => new PostListViewState();

}

class PostListViewState extends State<PostListView> {

   PostApi postapi;
   List <Post> getPostByCat1 = [];
   List <Post> getPostByCat2 = [];

   @override
   void initState() {
      super.initState();
      postapi = PostApi();
   }

   @override
   Widget build(BuildContext context) {

      return Scaffold(
      appBar: AppBar(
      title: Text('Post'),
      leading: new IconButton(
         icon: new Icon(Icons.arrow_back),
         onPressed: () { Navigator.pop(context, true); })),

      body: NestedScrollView(
        headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
        return [ SliverList(
           delegate: SliverChildListDelegate([
              Container(
                 child: Padding(
                    padding: EdgeInsets.only(left: 20.0, top: 30.0, right: 20.0, bottom: 30.0 ),
                    child: Column(
                    mainAxisSize: MainAxisSize.max,
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: <Widget>[
                       AutoSizeText( 'LATEST POST', maxLines: 2,
                       textAlign: TextAlign.center,
                       style: TextStyle (fontSize: 28, color: Colors.black, fontWeight: FontWeight.w700))
                    ])),
                    decoration: new BoxDecoration(
                        border: Border(
                          bottom: BorderSide(width: 1.0, color: Colors.grey ))))]))];},
        body: SafeArea(
            child: SizedBox(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Expanded(
                        child: ListView (
                            children: <Widget>[
                              Container(
                                  child: FutureBuilder(
                                    future: postapi.fetchAllPost(),
                                    builder: (BuildContext context, AsyncSnapshot snapshot) {

                                      if( snapshot.connectionState == ConnectionState.done ) {
                                        if( snapshot.hasError ) {
                                          return returnerror( snapshot.error.toString());
                                        }
                                      }
                                      if( snapshot.hasData) {
                                        return _postList( snapshot.data );
                                      }
                                      return Center ( child: CircularProgressIndicator());

                                    }))]))])))));
}

Widget _postList ( List<Post> posts ) {
   for ( var item in posts) {
     if (widget.name.contains(item.catname)) {
       getPostByCat1.add(item);
   }}
   return Container( child: _getPosts(getPostByCat1));
}

Widget _getPosts (List<Post> posts) {
  for(var item in getPostByCat1){
    if(widget.selectedIDS.contains(item.post_id))  {
      getPostByCat2.add(item);
    }
  } return Container(child: _listPosts(getPostByCat2));
}

Widget _listPosts (List<Post> posts) {
  return ListView.builder(
  physics: ClampingScrollPhysics(),
  shrinkWrap: true,
  itemCount: posts.length,
  itemBuilder: ( BuildContext context, int index ) {

  var imgUrl = ApiUtil.MAIN_API_URL + ApiUtil.IMAGE_PATH  + posts[index].image;

  String postID = posts[index].id;
  String postname = posts[index].name;

  return new Card (
     child: InkWell(
        onTap: () {
           Navigator.push(context, MaterialPageRoute(
              builder: (context) => PostDetails(postID, postname)));},
            child: Padding(
              padding: const EdgeInsets.all(10.0),
              child: new Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[

                    Expanded(
                      flex: 1,
                      child: Image.network(imgUrl),
                    ),

                    Expanded(
                      flex: 3,
                      child: Padding(
                          padding: EdgeInsets.all(10.0),
                          child: ListTile(
                            title: new Text(posts[index].name,
                              style: TextStyle(
                                  fontSize: 22,
                                  fontWeight: FontWeight.w700
                              ),
                            ),
                            subtitle: new Text(posts[index].subname,
                              style: TextStyle(
                                fontSize: 16,

  )))))])))); });}
 }
class PostListView扩展了StatefulWidget{
最终选定的列表ID;
最后的字符串名;
PostListView(this.selectedds,this.name);
@凌驾
PostListViewState createState()=>新建PostListViewState();
}
类PostListViewState扩展了状态{
PostApi PostApi;
列表getPostByCat1=[];
列表getPostByCat2=[];
@凌驾
void initState(){
super.initState();
postapi=postapi();
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“帖子”),
领先:新图标按钮(
图标:新图标(图标。箭头返回),
onPressed:(){Navigator.pop(context,true);})),
正文:嵌套滚动视图(
headerSliverBuilder:(BuildContext上下文,boolInnerBoxIsCrolled){
返回[切片列表](
委托:SliverChildListDelegate([
容器(
孩子:填充(
填充:仅限边缘设置(左侧:20.0,顶部:30.0,右侧:20.0,底部:30.0),
子:列(
mainAxisSize:mainAxisSize.max,
crossAxisAlignment:crossAxisAlignment.stretch,
儿童:[
AutoSizeText('LATEST POST',maxLines:2,
textAlign:textAlign.center,
样式:TextStyle(fontSize:28,颜色:Colors.black,fontWeight:fontWeight.w700))
])),
装饰:新盒子装饰(
边界:边界(
底部:边框边(宽度:1.0,颜色:Colors.grey))];},
正文:安全区(
孩子:大小盒子(
子:列(
mainAxisAlignment:mainAxisAlignment.space,
mainAxisSize:mainAxisSize.min,
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
扩大(
子:ListView(
儿童:[
容器(
孩子:未来建设者(
future:postapi.fetchAllPost(),
生成器:(BuildContext上下文,异步快照){
if(snapshot.connectionState==connectionState.done){
if(snapshot.hasError){
返回returnerror(snapshot.error.toString());
}
}
if(snapshot.hasData){
返回_postList(snapshot.data);
}
返回中心(子项:CircularProgressIndicator());
}))]))])))));
}
小部件_postList(列表帖子){
用于(职位中的var项目){
if(widget.name.contains(item.catname)){
getPostByCat1.add(项);
}}
返回容器(子项:_getPosts(getPostByCat1));
}
小部件_getPosts(列出帖子){
for(getPostByCat1中的变量项){
if(widget.selectedds.contains(item.post\u id)){
getPostByCat2.add(项);
}
}返回容器(子项:_listPosts(getPostByCat2));
}
小部件_列表帖子(列表帖子){
返回ListView.builder(
物理:ClampingScrollPhysics(),
收缩膜:对,
itemCount:posts.length,
itemBuilder:(构建上下文,int索引){
var imgUrl=ApiUtil.MAIN\u API\u URL+ApiUtil.IMAGE\u PATH+posts[index].IMAGE;
字符串postID=posts[index].id;
字符串postname=posts[index].name;
归还新卡(
孩子:InkWell(
onTap:(){
Navigator.push(上下文、MaterialPage路由)(
生成器:(上下文)=>PostDetails(postID,postname));},
孩子:填充(
填充:常数边集全部(10.0),
孩子:新的一排(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
扩大(
弹性:1,
子:Image.network(imgUrl),
),
扩大(
弹性:3,
孩子:填充(
填充:所有边缘设置(10.0),
孩子:ListTile(
标题:新文本(发布[索引])。名称,
样式:TextStyle(
尺寸:22,
fontWeight:fontWeight.w700
),
),
字幕:新文本(帖子[索引])。子名称,
样式:TextStyle(
尺寸:16,
)))))])))); });}
}

您的构建方法如何?以及你为什么返回未来。价值(真实);在pop?Hi Phuc之后,我更新了代码。