我正在构建一个社交网络类型的产品,其中用户具有关注者和“ workout_posts”。我创建了与Alex Mamo在回答Firestore - how to structure a feed and follow system时所概述的模型类似的模型。
我有一个用户集合,一个追随者集合和一个锻炼帖子集合。
我已经有效地设置了一个查询,以使用以下代码通过分页显示所有单个用户的帖子。这对于在应用程序的个人资料视图中显示所有单个用户的帖子非常有用。
class Workouts extends StatefulWidget {
Workouts({Key key, this.user}) : super(key: key);
final User user;
@override
_WorkoutsState createState() => new _WorkoutsState(
user: user
);
}
class _WorkoutsState extends State<Workouts> {
_WorkoutsState({this.user});
final User user;
Firestore _firestore = Firestore.instance;
List<DocumentSnapshot> _workouts = [];
bool _loadingWorkouts = true;
int _per_page = 8;
DocumentSnapshot _lastWorkout;
ScrollController _scrollController = ScrollController();
bool _gettingMoreWorkouts = false;
bool _moreWorkoutsAvailable = true;
_getWorkouts() async {
Query q = _firestore
.collection('workouts_posts')
.document(user.user_id)
.collection('posts')
.orderBy("post")
.limit(_per_page);
setState(() {
_loadingWorkouts = true;
});
QuerySnapshot querySnapshot = await q.getDocuments();
_workouts = querySnapshot.documents;
_lastWorkout = querySnapshot.documents[querySnapshot.documents.length - 1];
setState(() {
_loadingWorkouts = false;
});
}
_getMoreProducts() async {
print("get more products called");
if (_moreWorkoutsAvailable == false) {
return;
}
if (_gettingMoreWorkouts == true) {
return;
}
_gettingMoreWorkouts = true;
Query q = _firestore
.collection('workouts_posts')
.document(user.user_id)
.collection('posts')
.orderBy("post")
.startAfter([_lastWorkout.data['post']]).limit(_per_page);
QuerySnapshot querySnapshot = await q.getDocuments();
if (querySnapshot.documents.length < _per_page) {
_moreWorkoutsAvailable = false;
}
_lastWorkout = querySnapshot.documents[querySnapshot.documents.length - 1];
_workouts.addAll(querySnapshot.documents);
setState(() {});
_gettingMoreWorkouts = false;
}
@override
void initState() {
super.initState();
_getWorkouts();
_scrollController.addListener(() {
double maxScroll = _scrollController.position.maxScrollExtent;
double currentScroll = _scrollController.position.pixels;
double delta = MediaQuery.of(context).size.height * 0.25;
if (maxScroll - currentScroll < delta) {
_getMoreProducts();
}
});
}
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
new Expanded(
child: _loadingWorkouts == true
? Container(
child: Center(
child: Text("Loading..."),
),
)
: Container(
child: Center(
child: _workouts.length == 0
? Center(
child: Text("No Workouts to show"),
)
: ListView.builder(
controller: _scrollController,
itemCount: _workouts.length,
itemBuilder: (BuildContext ctx, int index) {
return new PostWidget(
postText: _workouts[index].data['post'],
pictureURL: _workouts[index].data['url'],
goalText: _workouts[index].data['goalText'],
userOfPost: _workouts[index].data['user'],
currentUser: user,
goalType: "workouts",
postID: _workouts[index].documentID,
);
}),
),
),
)
],
);
}
}
我的挑战是如何查询每个用户的所有关注者的帖子,将它们组合到一个列表中,然后按时间戳排序-所有这些都必须保持分页。是否有人有这样做的经验或对方法的建议?我应该更改数据模型以使其规范化吗?