我有两种类型的类型过滤,以获取帖子列表,然后传递到详细信息页面。我创建了新列表来获取帖子。
// 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 ....
}
我可以完美显示帖子列表,并推送到帖子详细信息页面。但是,当我从“帖子详细信息”页面按回时,该帖子条目是重复的。
下面是我的导航员传递的帖子ID
Navigator.push(context, MaterialPageRoute(
builder: (context) => PostDetails(postID)));
和后面的Onpress按钮。
onPressed: () {
Navigator.pop(context, true);
return Future.value(true);
}
实现它的最佳方法是什么?谢谢。 下面是帖子的ListView。
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,
)))))])))); });}
}