0

My screen is shown within a FutureBuilder and takes in a usersMap and a postsMapsList. I have a function that's called in initState which takes all the users and post out of those maps and turns them into lists of custom objects. In a widget I have I use UserSearchResult user = usersList.where((u) => u.uid == postsList[index].uid).single; to get the user object for a post but it prints State: no element. After some debugging, I realized that no posts are being added to the postsList which makes no sense because a different screen uses the same function to add posts and it works. I appreciate any ideas on how to fix this.

The postsMaps list consists of multiple maps containing posts (each user has a map of posts). So in this function, I take each map then each post in it, and create a Post object with it. I tried using forEach and for in for the list but both ways don't work.

Here's the relevent snippet of the function:

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);
          });
        }
      });
    });

Error:

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

Line where the error is:

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

Printed 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}]

Printed 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}]
Globe
  • 514
  • 3
  • 17

1 Answers1

0
if (postsList.where((e) => e.postId == post.postId).toList().isEmpty)

for this code, you are trying to check if the postList is contains post or not. if not, then add the post to the postList, is it right?

here another elegant workaround:

// 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;
}

then now we can compare 2 object Post,

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

update

  • What caused error: the single
usersList.where((u) => u.uid == postsList[index].uid).single;
  • why? because you use the single for your search user. There's a condition when result is null (not match any user) or maybe more than 1.
E get single {
  Iterator<E> it = iterator;
  if (!it.moveNext()) throw IterableElementError.noElement();
  • workaroud: opsi 1: use firstWhere
UserSearchResult? user = usersList.firstWhere((u) => u.uid == postsList[index].uid , orElse: () => 'No matching User found');
  • opsi 2 : use package:collection
import 'package:collection';
...

UserSearchResult? user = usersList.firstWhereOrNull((u) => u.uid == postsList[index].uid );

if no match user , then it will return null;

hope its solve your issue.

pmatatias
  • 3,491
  • 3
  • 10
  • 30
  • Thanks for the tip about checking is `postsList` contains `post`, I just added that! However, none of this solved the issue. I added the error and where it occurs. What's confusing to me is that I just got the `posts` adding to `postsList` so the element is there when I print the list but I still get the `Bad State: No element`. – Globe Sep 22 '22 at 02:40
  • now i get it, after you provde the error in console log. it caused your List is `nullable` value. I will update my aswer for detail – pmatatias Sep 22 '22 at 02:50
  • The posts print as expected... the app is in development stage so there are only 2 accounts and 6 posts total so I can easily see that each post has a corresponding account so there is no reason for `usersList.where((u) => u.uid == postsList[index].uid).single;` to show no element, the printed lists quite literally show that the element does exist. – Globe Sep 22 '22 at 03:13
  • its not only depend on your data, but also depend on data type. – pmatatias Sep 22 '22 at 03:16
  • I just added what `postsList` and `usersList` look like printed. That shows that every post has a user that exists. At this point, the data is in objects, not maps, so I know the data function works right. – Globe Sep 22 '22 at 03:29