Flutter坏状态:没有元素-地图中的对象列表不起作用



我的屏幕显示在FutureBuilder中,并接收usersMappostsMapsList。我有一个在initState中调用的函数,它将所有用户和帖子从这些映射中提取出来,并将它们转换为自定义对象列表。在我拥有的一个小部件中,我使用UserSearchResult user = usersList.where((u) => u.uid == postsList[index].uid).single;来获取帖子的用户对象,但它打印State: no element。经过一些调试,我意识到postsList中没有添加任何帖子,这毫无意义,因为不同的屏幕使用相同的功能来添加帖子,并且它可以工作。我很感激任何关于如何解决这个问题的想法。

postsMaps列表由多个包含帖子的地图组成(每个用户都有一个帖子地图(。因此,在这个函数中,我先获取每个映射,然后再获取其中的每个帖子,并用它创建一个Post对象。我尝试使用forEachfor in作为列表,但这两种方法都不起作用。

以下是函数的相关片段:

postsMaps.forEach((postsMap) {
postsMap.forEach((index, value) {
setState(() {
postsLoading = true;
});
final post = Post.fromJson(value.data() as Map<String, dynamic>);
print('Post: ${value.data()}');
if (postsList
.where((e) => e.postId == post.postId)
.toList()
.isEmpty) {
setState(() {
postsList.add(post);
});
}
});
});

错误:

The following StateError was thrown building HomeScreen(dirty, state: _HomeScreenState#9ef08):
Bad state: No element
The relevant error-causing widget was: 
HomeScreen HomeScreen:file:///Users/cjm/AndroidStudioProjects/globe_rebrand/lib/screens/main/tab_bar_view.dart:126:24
When the exception was thrown, this was the stack: 
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 266:49      throw_
dart-sdk/lib/core/iterable.dart 654:63                                            get single
packages/globe_rebrand/screens/main/home_screen.dart 123:71                       build

错误所在的行:

UserSearchResult user = usersList.where((u) => u.uid == postsList[index].uid).single;

打印postList:

postsList: [Post: {postId: 1663650914746, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/PeDVktc9O1bbhMbewRrie8taQ4E3%2Fposts%2F1663650914746%2F%5B%238be79%5D_post.png?alt=media&token=ae1f1423-2de5-444d-baa7-6c59f85f7067], uid: PeDVktc9O1bbhMbewRrie8taQ4E3}, Post: {postId: 1663804907974, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/PeDVktc9O1bbhMbewRrie8taQ4E3%2Fposts%2F1663804907974%2F%5B%23c0252%5D_post.png?alt=media&token=d17bfbb6-be14-4536-b203-4ed83a76b5cb], uid: PeDVktc9O1bbhMbewRrie8taQ4E3}, Post: {postId: 1663650914746, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/PeDVktc9O1bbhMbewRrie8taQ4E3%2Fposts%2F1663650914746%2F%5B%238be79%5D_post.png?alt=media&token=ae1f1423-2de5-444d-baa7-6c59f85f7067], uid: PeDVktc9O1bbhMbewRrie8taQ4E3}, Post: {postId: 1663804907974, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/PeDVktc9O1bbhMbewRrie8taQ4E3%2Fposts%2F1663804907974%2F%5B%23c0252%5D_post.png?alt=media&token=d17bfbb6-be14-4536-b203-4ed83a76b5cb], uid: PeDVktc9O1bbhMbewRrie8taQ4E3}, Post: {postId: 1663650914746, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/PeDVktc9O1bbhMbewRrie8taQ4E3%2Fposts%2F1663650914746%2F%5B%238be79%5D_post.png?alt=media&token=ae1f1423-2de5-444d-baa7-6c59f85f7067], uid: PeDVktc9O1bbhMbewRrie8taQ4E3}, Post: {postId: 1663804907974, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/PeDVktc9O1bbhMbewRrie8taQ4E3%2Fposts%2F1663804907974%2F%5B%23c0252%5D_post.png?alt=media&token=d17bfbb6-be14-4536-b203-4ed83a76b5cb], uid: PeDVktc9O1bbhMbewRrie8taQ4E3}, Post: {postId: 1663650914746, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/PeDVktc9O1bbhMbewRrie8taQ4E3%2Fposts%2F1663650914746%2F%5B%238be79%5D_post.png?alt=media&token=ae1f1423-2de5-444d-baa7-6c59f85f7067], uid: PeDVktc9O1bbhMbewRrie8taQ4E3}, Post: {postId: 1663804907974, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/PeDVktc9O1bbhMbewRrie8taQ4E3%2Fposts%2F1663804907974%2F%5B%23c0252%5D_post.png?alt=media&token=d17bfbb6-be14-4536-b203-4ed83a76b5cb], uid: PeDVktc9O1bbhMbewRrie8taQ4E3}, Post: {postId: 1662259492485, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1662259492485%2F%5B%234f06c%5D_post.png?alt=media&token=c3daa4b9-b1b7-4ac7-99ba-1c9f0872e77a, https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1662259492485%2F%5B%23dc27b%5D_post.png?alt=media&token=c21a4a15-c729-4c94-a9a6-f0ed88e4d57e], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663378122899, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663378122899%2F%5B%2392b8b%5D_post.png?alt=media&token=6bdc32a9-76c8-4f42-85ed-7659fe63b652], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663484428194, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663484428194%2F%5B%2319e71%5D_post.png?alt=media&token=5ef84c5c-00f8-4996-9134-71b368247ba6], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663548532558, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663548532558%2F%5B%2303070%5D_post.png?alt=media&token=320805f6-eb2a-4518-af4c-ea5b9e9217dd], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663644750049, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663644750049%2F%5B%23653c1%5D_post.png?alt=media&token=3ebfb68e-214e-4e04-a77e-fc4ec7333123], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1662259492485, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1662259492485%2F%5B%234f06c%5D_post.png?alt=media&token=c3daa4b9-b1b7-4ac7-99ba-1c9f0872e77a, https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1662259492485%2F%5B%23dc27b%5D_post.png?alt=media&token=c21a4a15-c729-4c94-a9a6-f0ed88e4d57e], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663378122899, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663378122899%2F%5B%2392b8b%5D_post.png?alt=media&token=6bdc32a9-76c8-4f42-85ed-7659fe63b652], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663484428194, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663484428194%2F%5B%2319e71%5D_post.png?alt=media&token=5ef84c5c-00f8-4996-9134-71b368247ba6], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663548532558, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663548532558%2F%5B%2303070%5D_post.png?alt=media&token=320805f6-eb2a-4518-af4c-ea5b9e9217dd], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663644750049, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663644750049%2F%5B%23653c1%5D_post.png?alt=media&token=3ebfb68e-214e-4e04-a77e-fc4ec7333123], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1662259492485, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1662259492485%2F%5B%234f06c%5D_post.png?alt=media&token=c3daa4b9-b1b7-4ac7-99ba-1c9f0872e77a, https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1662259492485%2F%5B%23dc27b%5D_post.png?alt=media&token=c21a4a15-c729-4c94-a9a6-f0ed88e4d57e], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663378122899, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663378122899%2F%5B%2392b8b%5D_post.png?alt=media&token=6bdc32a9-76c8-4f42-85ed-7659fe63b652], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663484428194, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663484428194%2F%5B%2319e71%5D_post.png?alt=media&token=5ef84c5c-00f8-4996-9134-71b368247ba6], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663548532558, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663548532558%2F%5B%2303070%5D_post.png?alt=media&token=320805f6-eb2a-4518-af4c-ea5b9e9217dd], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663644750049, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663644750049%2F%5B%23653c1%5D_post.png?alt=media&token=3ebfb68e-214e-4e04-a77e-fc4ec7333123], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1662259492485, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1662259492485%2F%5B%234f06c%5D_post.png?alt=media&token=c3daa4b9-b1b7-4ac7-99ba-1c9f0872e77a, https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1662259492485%2F%5B%23dc27b%5D_post.png?alt=media&token=c21a4a15-c729-4c94-a9a6-f0ed88e4d57e], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663378122899, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663378122899%2F%5B%2392b8b%5D_post.png?alt=media&token=6bdc32a9-76c8-4f42-85ed-7659fe63b652], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663484428194, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663484428194%2F%5B%2319e71%5D_post.png?alt=media&token=5ef84c5c-00f8-4996-9134-71b368247ba6], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663548532558, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663548532558%2F%5B%2303070%5D_post.png?alt=media&token=320805f6-eb2a-4518-af4c-ea5b9e9217dd], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663644750049, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663644750049%2F%5B%23653c1%5D_post.png?alt=media&token=3ebfb68e-214e-4e04-a77e-fc4ec7333123], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}]

打印usersList:

usersList: [Result: {profile: Profile: {profileInfo: Profile: {pfpUrl: https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2F1662335024351_pfp.png?alt=media&token=90dda6d3-5875-45f6-bce8-d5c2f2a2dca2, displayName: name}, posts: [Post: {postId: 1662259492485, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1662259492485%2F%5B%234f06c%5D_post.png?alt=media&token=c3daa4b9-b1b7-4ac7-99ba-1c9f0872e77a, https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1662259492485%2F%5B%23dc27b%5D_post.png?alt=media&token=c21a4a15-c729-4c94-a9a6-f0ed88e4d57e], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663378122899, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663378122899%2F%5B%2392b8b%5D_post.png?alt=media&token=6bdc32a9-76c8-4f42-85ed-7659fe63b652], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663484428194, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663484428194%2F%5B%2319e71%5D_post.png?alt=media&token=5ef84c5c-00f8-4996-9134-71b368247ba6], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663548532558, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663548532558%2F%5B%2303070%5D_post.png?alt=media&token=320805f6-eb2a-4518-af4c-ea5b9e9217dd], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663644750049, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663644750049%2F%5B%23653c1%5D_post.png?alt=media&token=3ebfb68e-214e-4e04-a77e-fc4ec7333123], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1662259492485, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1662259492485%2F%5B%234f06c%5D_post.png?alt=media&token=c3daa4b9-b1b7-4ac7-99ba-1c9f0872e77a, https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1662259492485%2F%5B%23dc27b%5D_post.png?alt=media&token=c21a4a15-c729-4c94-a9a6-f0ed88e4d57e], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663378122899, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663378122899%2F%5B%2392b8b%5D_post.png?alt=media&token=6bdc32a9-76c8-4f42-85ed-7659fe63b652], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663484428194, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663484428194%2F%5B%2319e71%5D_post.png?alt=media&token=5ef84c5c-00f8-4996-9134-71b368247ba6], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663548532558, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663548532558%2F%5B%2303070%5D_post.png?alt=media&token=320805f6-eb2a-4518-af4c-ea5b9e9217dd], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663644750049, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663644750049%2F%5B%23653c1%5D_post.png?alt=media&token=3ebfb68e-214e-4e04-a77e-fc4ec7333123], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1662259492485, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1662259492485%2F%5B%234f06c%5D_post.png?alt=media&token=c3daa4b9-b1b7-4ac7-99ba-1c9f0872e77a, https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1662259492485%2F%5B%23dc27b%5D_post.png?alt=media&token=c21a4a15-c729-4c94-a9a6-f0ed88e4d57e], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663378122899, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663378122899%2F%5B%2392b8b%5D_post.png?alt=media&token=6bdc32a9-76c8-4f42-85ed-7659fe63b652], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663484428194, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663484428194%2F%5B%2319e71%5D_post.png?alt=media&token=5ef84c5c-00f8-4996-9134-71b368247ba6], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663548532558, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663548532558%2F%5B%2303070%5D_post.png?alt=media&token=320805f6-eb2a-4518-af4c-ea5b9e9217dd], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663644750049, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663644750049%2F%5B%23653c1%5D_post.png?alt=media&token=3ebfb68e-214e-4e04-a77e-fc4ec7333123], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1662259492485, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1662259492485%2F%5B%234f06c%5D_post.png?alt=media&token=c3daa4b9-b1b7-4ac7-99ba-1c9f0872e77a, https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1662259492485%2F%5B%23dc27b%5D_post.png?alt=media&token=c21a4a15-c729-4c94-a9a6-f0ed88e4d57e], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663378122899, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663378122899%2F%5B%2392b8b%5D_post.png?alt=media&token=6bdc32a9-76c8-4f42-85ed-7659fe63b652], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663484428194, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663484428194%2F%5B%2319e71%5D_post.png?alt=media&token=5ef84c5c-00f8-4996-9134-71b368247ba6], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663548532558, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663548532558%2F%5B%2303070%5D_post.png?alt=media&token=320805f6-eb2a-4518-af4c-ea5b9e9217dd], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Post: {postId: 1663644750049, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/HqtGLN1c32VHhCn2n2To6gC2qbA2%2Fposts%2F1663644750049%2F%5B%23653c1%5D_post.png?alt=media&token=3ebfb68e-214e-4e04-a77e-fc4ec7333123], uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}]}, uid: HqtGLN1c32VHhCn2n2To6gC2qbA2}, Result: {profile: Profile: {profileInfo: Profile: {pfpUrl: https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/PeDVktc9O1bbhMbewRrie8taQ4E3%2F%5B%2390398%5D_pfp.png?alt=media&token=681b3bf4-3dc4-4062-a1b0-875d0425c90b, displayName: username}, posts: [Post: {postId: 1663650914746, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/PeDVktc9O1bbhMbewRrie8taQ4E3%2Fposts%2F1663650914746%2F%5B%238be79%5D_post.png?alt=media&token=ae1f1423-2de5-444d-baa7-6c59f85f7067], uid: PeDVktc9O1bbhMbewRrie8taQ4E3}, Post: {postId: 1663804907974, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/PeDVktc9O1bbhMbewRrie8taQ4E3%2Fposts%2F1663804907974%2F%5B%23c0252%5D_post.png?alt=media&token=d17bfbb6-be14-4536-b203-4ed83a76b5cb], uid: PeDVktc9O1bbhMbewRrie8taQ4E3}, Post: {postId: 1663650914746, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/PeDVktc9O1bbhMbewRrie8taQ4E3%2Fposts%2F1663650914746%2F%5B%238be79%5D_post.png?alt=media&token=ae1f1423-2de5-444d-baa7-6c59f85f7067], uid: PeDVktc9O1bbhMbewRrie8taQ4E3}, Post: {postId: 1663804907974, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/PeDVktc9O1bbhMbewRrie8taQ4E3%2Fposts%2F1663804907974%2F%5B%23c0252%5D_post.png?alt=media&token=d17bfbb6-be14-4536-b203-4ed83a76b5cb], uid: PeDVktc9O1bbhMbewRrie8taQ4E3}, Post: {postId: 1663650914746, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/PeDVktc9O1bbhMbewRrie8taQ4E3%2Fposts%2F1663650914746%2F%5B%238be79%5D_post.png?alt=media&token=ae1f1423-2de5-444d-baa7-6c59f85f7067], uid: PeDVktc9O1bbhMbewRrie8taQ4E3}, Post: {postId: 1663804907974, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/PeDVktc9O1bbhMbewRrie8taQ4E3%2Fposts%2F1663804907974%2F%5B%23c0252%5D_post.png?alt=media&token=d17bfbb6-be14-4536-b203-4ed83a76b5cb], uid: PeDVktc9O1bbhMbewRrie8taQ4E3}, Post: {postId: 1663650914746, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/PeDVktc9O1bbhMbewRrie8taQ4E3%2Fposts%2F1663650914746%2F%5B%238be79%5D_post.png?alt=media&token=ae1f1423-2de5-444d-baa7-6c59f85f7067], uid: PeDVktc9O1bbhMbewRrie8taQ4E3}, Post: {postId: 1663804907974, imageUrls: [https://firebasestorage.googleapis.com/v0/b/globe-rebrand.appspot.com/o/PeDVktc9O1bbhMbewRrie8taQ4E3%2Fposts%2F1663804907974%2F%5B%23c0252%5D_post.png?alt=media&token=d17bfbb6-be14-4536-b203-4ed83a76b5cb], uid: PeDVktc9O1bbhMbewRrie8taQ4E3}]}, uid: PeDVktc9O1bbhMbewRrie8taQ4E3}]
if (postsList.where((e) => e.postId == post.postId).toList().isEmpty)

对于此代码,您将尝试检查postList是否包含post。如果不是,那么把post加到postList上,对吗?

这里有另一个优雅的解决方法:

// in your Post model, add this code.
// this code added , so we can able to compare 2 object of Post
class Post {
.....
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Post &&
runtimeType == other.runtimeType &&
productId == other.id;
@override
int get hashCode => id.hashCode;
}

那么现在我们可以比较两个对象Post,

.....
if (postsList.contains(post)){
setState(() {
postsList.add(post);
});
}
print(postList); // you must see the post just added to postList

更新

  • 导致错误的原因:single
usersList.where((u) => u.uid == postsList[index].uid).single;
  • 为什么?因为您将single用于搜索用户。当结果为null(不匹配任何用户(或可能大于1时,会出现一个条件
E get single {
Iterator<E> it = iterator;
if (!it.moveNext()) throw IterableElementError.noElement();
  • 工作组:opsi 1:使用firstWhere
UserSearchResult? user = usersList.firstWhere((u) => u.uid == postsList[index].uid , orElse: () => 'No matching User found');
  • opsi 2:使用包:集合
import 'package:collection';
...
UserSearchResult? user = usersList.firstWhereOrNull((u) => u.uid == postsList[index].uid );

如果没有匹配的用户,则返回null;

希望它能解决你的问题。

最新更新