2

How do I save the state of a dismissible? When the item is dismissed it stays dismissed upon closing and opening the app?

I'm relatively new to flutter and checked out saved preferences plugin but couldn't get it to work.

Tom O'Sullivan
  • 3,416
  • 5
  • 15
  • 27
  • How is your list of `Dismissible` items being created; what's the data source? In the `onDismissed` handler you'll need to update that data source to remove the item that was dismissed and persist it again, so that the next app load is consuming the reduced data set. If you can show some code, I can give you a bit more direction :) – Derek Lakin Jul 27 '18 at 16:45
  • @DerekLakin that would be perfect, I seem to be okay at the UI side of dart now but struggle a bit with functionality. I'll some code when I get a chance later this weekend or next week, but I use a SliverChildBuilderDelgate with a child dismissible, and the information is from a List String, so nothing too fancy as its basic info I need. I had a SetState to remove the item, but can't get it to persist. – Tom O'Sullivan Jul 28 '18 at 07:10
  • Where does the `List` exist in relation to your widget and how is it declared? Remember that the `build` method will be called each time the state changes, so if your list is local to the `build` method it will be recreated the same way as it was the first time (apologies if I'm stating the obvious). – Derek Lakin Jul 30 '18 at 08:21
  • @DerekLakin that makes a lot more sense on why I'm getting this issue of it recreating! Whats the best way to go about adding data to a dismissible for its data to stay dismissed? I've been going round in circles, is there an example anywhere? :) – Tom O'Sullivan Aug 07 '18 at 08:47

1 Answers1

1

You need some way to persist and restore the state of the application between launches. A common solution in Flutter is to use Redux, which is a unidirectional data flow architecture for managing state. This blog post from Xavi Rigau is quite a good introduction. There is an example app in the GitHub repository that extends the default counter example app to use redux.

In order to persist the state between app launches, you'll need to add the persistence layer. One option would be to use your own file storage middleware (see Xavi's post and the examples in the flutter_redux repository). Another approach would be to use the Flutter redux_persist library (I haven't used this one yet).

Note: Given the complexities and details involved, I haven't included code examples here, but there is plenty in the listed references.

Derek Lakin
  • 16,179
  • 36
  • 51