如何解决对Flutter应用程序的getX dart包中的RxList进行排序
我无法通过给定属性对可观察的RxList进行排序。当我使用BloC方法并使用一个简单的List时,排序工作正常。
最近,我将状态管理更改为GetX(https://github.com/jonataslaw/getx)程序包,因为看到了很多优点(不需要上下文,无需过渡到页面,等等)。
这里有两种情况,我很糟糕,之前没有解释,让我放入小黄瓜的
场景1 :
Given user in Posts Screen
When user Pull down to get Newest posts
Then user get the newly fetched posts on top of the previous loaded list
方案2:
Given user in Posts Screen
When user reaches the end of the list
Then user get more posts at the end of the previous loaded list
post_model.dart
class Post {
final int id;
final String title;
final DateTime date;
final User user;
Post({
@required this.id,@required this.title,@required this.date,this.user});
}
post_controller.dart
import 'dart:async';
import 'package:get/state_manager.dart';
import 'package:blog/models/post.dart';
import 'package:blog/services/posts_api.dart';
class PostsController extends GetxController {
int _page = 0;
---------HERE OBS LIST-----------
RxList posts = [].obs;
@override
void onReady() {
super.onReady();
this.loadPosts();
}
Future<void> loadPosts({isRefreshLoad = false}) async {
int pager;
if (isRefreshLoad) {
pager = 1;
} else {
increment();
pager = this._page;
}
Map<String,String> arguments = {'page': '$pager'};
final List<Post> newPosts =
await PostsApi.instance.getPosts(arguments: arguments);
// remove duplicates
posts.addAll(
newPosts.where(
(newPosts) => posts.every((post) => newPosts.id != post.id),),);
//----------HERE'S THE EXCEPTION----------
posts.sort((a,b) => b.id.compareTo(a.id));
update(['posts']);
}
}
错误
flutter: [GETX] PostsController has been initialized
[VERBOSE-2:ui_dart_state.cc(166)] Unhandled Exception: type 'Post' is not a subtype of type 'List<dynamic>'
#0 RxList.remove
package:get/…/rx/rx_impl.dart:333
PostsScreen.dart
Scaffold(
backgroundColor: Color.fromRGBO(58,66,86,1.0),appBar: AppBarWidget(
drawerOpen: drawerPosition,appBarVisible: _isVisible,drawerVisible: true,body: ColorfulSafeArea(
overflowRules: OverflowRules.only(left: true,bottom: true),child: GetBuilder<PostsController>(
id: 'posts',init: PostsController(),builder: (_postsController) {
_postsControllerGlobal = _postsController;
// Scenario 1 - Triggers when user pull down to get new posts
return RefreshIndicator(
onRefresh: () {
return _postsController.loadPosts(
isRefreshLoad: true);
},child: StreamBuilderWidget(
stream: _postsController.posts.stream,// Scenario 2 Triggers when scroll controller reaches end of list
scrollController: _scrollController,isDrawerOpen: _isDrawerOpen,widgetType: 'PostItemWidget',);
},
---- {enter code here
------
我需要一种方法,将触发器添加到流中后,始终按日期降序对获取的帖子列表进行重新排序,无论触发器是PullDown还是到达列表的末尾,以便始终显示项按发布顺序。
希望有人可以帮帮我。
解决方法
首先订购您的列表,然后添加它
final List<Post> newPosts =
await PostsApi.instance.getPosts(arguments: arguments);
//----------Try this---------
newPosts.sort((a,b) => b.id.compareTo(a.id));
// remove duplicates
posts.addAll(
newPosts.where(
(newPosts) => posts.every((post) => newPosts.id != post.id),),);
,
经过数天的努力,我发现它简单得多,并且由于我缺乏知识。
由于列表是可见的,仅将其转换为列表就足以完成排序:
普通列表
- newPosts.sort((a,b)=> b.date.compareTo(a.date));
可观察列表
- newPosts.toList()。sort((a,b)=> b.date.compareTo(a.date));
谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。