Biagio63
Biagio63

Reputation: 11

Flutter and Riverpod. Rebuild paginatedDataTable from another page don't works

I have this problem that has been blocking me for days.

My application has two pages: Page A - paginatedDataTable (reads records from productsGridProvider) Page B - Form to edit or add a record.(reads 1 record from productFormProvider)

When I add a record from page B and ask to update the paginatedDataTable don't works.

// From page B

TextButton.icon(
            style: TextButton.styleFrom(
                shape: const RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.zero))),
            label: const Text("Save"),
            icon: const Icon(
              Icons.save,
            ),
            onPressed: () {
              if (_formKey.currentState!.validate()) {
                if (ref.read(productFormProvider(nameStack)).act != "view") {
                  ref.read(productFormProvider(nameStack)).save(ref);
                  ref.watch(productsGridProvider.notifier).update();
                } else {
                  null;
                }
              }
            }),

// In ProductsGridProvider

class ProductsGridViewModel extends StateNotifier<Future<List<Product>>> {
  ProductsGridViewModel() : super(Future(() => [])) {
    state = fetchProducts();
  }

...

void update() {
    state = fetchProducts();
  }


If I create Page C (joining A + B), using the same code, everything works fine.

I don't understand where I'm wrong. I don't understand why calling the same code I get two different results

I'm sorry for my english.

Thanks in advance

Biagio

Upvotes: 0

Views: 176

Answers (1)

Ruble
Ruble

Reputation: 4844

These simple steps will lead you to a working solution:

  • use AsyncNotifier for your product list (instead of StateNotifier), and
  • do NOT use ref.watch in the callback function (use ref.read).

Upvotes: 0

Related Questions