Skip to content

Bad State: No Element #4

@zionnite

Description

@zionnite

Hello sir,
first i want to thank you for this long hours of course.
sir, have followed the video and i hav being able to do something wonderful but how ever i keep geting an error, below is screenshot and with my code.

Simulator Screen Shot - iPhone 14 Pro Max - 2023-09-05 at 01 52 10

ref.watch(getRepliesToPostProvider(widget.postModel!)).when(
                  data: (post) {
                    return ref.watch(getLatestPostProvider).when(
                        data: (data) {
                          if (data.events.contains(
                            'databases.*.collections.${Common.postCollection}.documents.*.create',
                          )) {
                            post.insert(0, PostModel.fromMap(data.payload));
                          } else if (data.events.contains(
                            'databases.*.collections.${Common.postCollection}.documents.*.update',
                          )) {
                            final startingIndex =
                                data.events[0].lastIndexOf('documents.');
                            final endIndex =
                                data.events[0].lastIndexOf('.update');

                            var postId = data.events[0]
                                .substring(startingIndex + 10, endIndex);

                            var newPost = post
                                .where((element) => element.id == postId)
                                .first;

                            final postIndex = post.indexOf(newPost);
                            post.removeWhere((element) => element.id == postId);

                            newPost = PostModel.fromMap(data.payload);
                            post.insert(postIndex, newPost);
                          }

                          return Expanded(
                            child: ListView.builder(
                              padding: const EdgeInsets.all(0),
                              shrinkWrap: true,
                              scrollDirection: Axis.vertical,
                              physics: const ClampingScrollPhysics(),
                              itemCount: post.length,
                              itemBuilder: (BuildContext context, int index) {
                                var disPost = post[index];

                                final latestPost =
                                    PostModel.fromMap(data.payload);
                                bool isPostAlreadyPresent = false;
                                for (final postLoop in post) {
                                  if (postLoop.id == latestPost.id) {
                                    isPostAlreadyPresent = true;
                                    break;
                                  }
                                }

                                return ref
                                    .watch(userDetailsProvider(
                                        widget.postModel!.uid))
                                    .when(
                                        data: (user) {
                                          return Padding(
                                            padding: const EdgeInsets.only(
                                              left: 8.0,
                                              top: 2,
                                              right: 8,
                                              bottom: 2,
                                            ),
                                            child: Row(
                                              children: [
                                                CircularProfileAvatar(
                                                  user.profile,
                                                  radius: 20,
                                                  backgroundColor:
                                                      Colors.transparent,
                                                  borderWidth: 2,
                                                  initialsText: Text(
                                                    "AD",
                                                    style: TextStyle(
                                                        color: colorWhite),
                                                  ),
                                                  borderColor: colorWhite,
                                                  elevation: 5.0,
                                                  foregroundColor: Colors.brown
                                                      .withOpacity(0.5),
                                                  cacheImage: true,
                                                  imageFit: BoxFit.cover,
                                                  onTap: () {
                                                    print('adil');
                                                  },
                                                  showInitialTextAbovePicture:
                                                      false,
                                                ),
                                                const SizedBox(
                                                  width: 15,
                                                ),
                                                Expanded(
                                                  child: Text(disPost.text),
                                                ),
                                              ],
                                            ),
                                          );
                                        },
                                        error: (error, stk) => Container(),
                                        loading: () => Container());
                              },
                            ),
                          );
                        },
                        error: (error, stk) => ErrorText(
                              error: error.toString(),
                            ),
                        loading: () {
                          return Expanded(
                            child: ListView.builder(
                                padding: const EdgeInsets.all(0),
                                shrinkWrap: true,
                                scrollDirection: Axis.vertical,
                                physics: const ClampingScrollPhysics(),
                                itemCount: post.length,
                                itemBuilder: (BuildContext context, int index) {
                                  var disPost = post[index];

                                  return ref
                                      .watch(userDetailsProvider(
                                          widget.postModel!.uid))
                                      .when(
                                          data: (user) {
                                            return Padding(
                                              padding: const EdgeInsets.only(
                                                left: 8.0,
                                                top: 2,
                                                right: 8,
                                                bottom: 2,
                                              ),
                                              child: Row(
                                                children: [
                                                  CircularProfileAvatar(
                                                    user.profile,
                                                    radius: 20,
                                                    backgroundColor:
                                                        Colors.transparent,
                                                    borderWidth: 2,
                                                    initialsText: Text(
                                                      "AD",
                                                      style: TextStyle(
                                                          color: colorWhite),
                                                    ),
                                                    borderColor: colorWhite,
                                                    elevation: 5.0,
                                                    foregroundColor: Colors
                                                        .brown
                                                        .withOpacity(0.5),
                                                    cacheImage: true,
                                                    imageFit: BoxFit.cover,
                                                    onTap: () {
                                                      print('adil');
                                                    },
                                                    showInitialTextAbovePicture:
                                                        false,
                                                  ),
                                                  const SizedBox(
                                                    width: 15,
                                                  ),
                                                  Expanded(
                                                    child: Text(disPost.text),
                                                  ),
                                                ],
                                              ),
                                            );
                                          },
                                          error: (error, stk) => Container(),
                                          loading: () => Container());
                                }),
                          );
                        });
                  },
                  error: (error, stk) => ErrorText(
                    error: error.toString(),
                  ),
                  loading: () => const Loader(),
                ),

thank you very much

Screenshot 2023-09-05 at 1 53 01 AM
Screenshot 2023-09-05 at 1 53 35 AM

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions