Best practice for deleting rows with active listeners #3821
|
I have a Drift + Riverpod application that roughly follows the example implementation included in this repository. I have a UI where I list elements and then each of those elements has their own Riverpod provider where they fetch information about the element. The idea being that I can fetch information for each element as they are scrolled into view and don't have to load a large SQL query upfront. However, this causes issues when I go to delete one of those items in the list. The item is removed from the db but the Riverpod provider for the element complains about: ...this is expected because the row has been removed, but the widget is still in the tree for a bit it seems and thus I see these error logs. Is there a way to avoid this? What's the recommended architecture for lists of items that can be deleted (where each item in the list has its own store that it listens to for e.g. sub items)? Edit: I'm more than happy to provide more detailed code snippets if my high-level explanation was too vague. |
Replies: 1 comment 2 replies
|
Use the nullable single-row stream for a row that can disappear.
For a deletable list item provider, I would make the row-level provider return Keep |
Use the nullable single-row stream for a row that can disappear.
watchSingle()is documented as asserting that exactly one row is emitted each time the query runs, and the implementation iswatch().transform(singleElements()). So the error after delete is the intended invariant failure: the query is now returning zero rows while something is still listening.watchSingleOrNull()is the variant documented for empty result sets; it emitsnullinstead of treating the missing row as an error.For a deletable list item provider, I would make the row-level provider return
Stream<T?>fromwatchSingleOrNull()and treatnullas "this row has been deleted" in the UI/state layer.autoDisposeis still…