0

I'm trying to delete a List of objects (Posts), which comes from Flutter Cubit method getPosts(), but I am absolutely not able to get these data in the second method of the cubit deletePosts(). From what I've searched on the internet, it seems there is a variable called "state", which should contain all the data of the Cubit, but if I print the "state" variable, it returns just an empty array.

I am missing some basic thing, but I am out of ideas what it is. Any advice will be highly appreciated.

The code:

class PostsCubit extends Cubit<List<Post>> {
    final _dataService = DataService();

    PostsCubit() : super([]);

    // WORKS PERFECTLY - IT GETS ALL POSTS
    Future<void> getPosts() async {
        print('Getting posts');
        return emit(await _dataService.getPosts());
    }

    // CANNOT FIGURE OUT, HOW TO DELETE DATA FROM getPosts() (STATE OF THE CUBIT)
    void deletePosts() {
        print(state); // Prints empty array "[]"
        return emit(?????????);
        // return emit(state.clear()); // DOES NOT WORK
    }
}

The widget displaying posts:

return MaterialApp(
  debugShowCheckedModeBanner: false,
  title: 'Flutter Demo',
  theme: ThemeData(
    primarySwatch: Colors.blue,
  ),
  home: BlocProvider<PostsCubit>(
    create: (BuildContext context) {
      return PostsCubit()..getPosts();
    },
    child: PostsView(), // DISPLAYS ALL POSTS FROM getPosts() WITHOUT ANY PROBLEM
  ),
);
Kostnesm
  • 1
  • 2

2 Answers2

0

If you deleting all posts you can emit new empty array;

    void deletePosts() {
        return emit(const []);
    }
Kherel
  • 14,882
  • 5
  • 50
  • 80
  • Thank you Kherel, unfortunately there is still the same problem - the 'state' of the PostsCubit seems to be an empty array. So if I try to emit(const []); , it just does nothing - since it is the same value as the previous state. – Kostnesm Apr 21 '23 at 13:01
0

Since Bloc depends on your state parameters being immutable, you need to create a copy of the list first, then it will treat it as an updated state.

If you're trying to just wipe the entire list, you can just emit a new empty list

  void deletePosts() {
    return emit([]); // this will update the state to an empty list
  }

If you need to modify the list without wiping it, it could look like this

  void modifyPosts() {

   // this is a copy of the list, a new instance in memory, using the spread operator

   final updatedPostsList = [...state]; 

   updatedPostsList[0] = Post(
        /// some modification
        );
    return emit(updatedPostsList); // this will emit the modified list
  }
Loren.A
  • 4,872
  • 1
  • 10
  • 17
  • Thank you Loren.A. That would make perfectly sense, but unfortunately the state variable is still an empty array - so if I do suggested deletePosts(), it just does nothing and in modifyPosts(), there is an error, because the state is still an empty array and there is not an element with index [0]. So I still have no clue where the data (Posts) of the cubit are - I see them in the ListView, but I cannot get them inside the cubit. I feel like a monkey in a spaceship :)) – Kostnesm Apr 21 '23 at 12:44
  • @Kostnesm the modifying at index 0 was just an example of how to modify a list in a Bloc state. If the list is empty, then yes that would throw an expected error. With the way your Cubit is setup, you can access the list within a Cubit function with `state`. `state` is your list of posts because thats what you passed in with extends `Cubit>` – Loren.A Apr 21 '23 at 13:19
  • I tried your example locally and it works as expected. The `List` is accessible via `state` and the print, prints out all posts. You can try sharing your UI code to see if there's something going on there that is modifying the state. – Loren.A Apr 21 '23 at 13:32