gpt4 book ai didi

list - 从 getX dart 包中为 Flutter 应用程序排序 RxList

转载 作者:行者123 更新时间:2023-12-02 16:31:10 24 4
gpt4 key购买 nike

我无法按给定属性对可观察的 RxList 进行排序。当我使用 BloC 方法时,排序工作正常,使用一个简单的列表。

最近我将状态管理更改为 GetX ( https://github.com/jonataslaw/getx ) 包,因为我看到了几个优点(不需要上下文、页面转换等)。

这里有两种情况,我的不好,之前没有说明,让我放小 cucumber 的

场景 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',
),
);
},
),
),
),

----在此处输入代码------

我需要一种方法,无论触发器是 PullDown 还是到达列表的最后末尾,在它们被添加到流后始终按日期 desc 顺序对获取的帖子列表进行重新排序,以便始终显示项目按发布顺序。

希望有人能帮帮我。

最佳答案

经过几天的努力,我发现它要简单得多,而且由于我缺乏知识。

由于列表是可观察的,只需将其转换为列表就足以完成排序:

普通列表

  • newPosts.sort((a, b) => b.date.compareTo(a.date));

可观察列表

  • newPosts.toList().sort((a, b) => b.date.compareTo(a.date));

谢谢!

关于list - 从 getX dart 包中为 Flutter 应用程序排序 RxList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63473890/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com