1

I know this question have been asked by many, its related to index of list to be viewd in grid view. The issue of mine is combining GridView.count() with Obx() using Getx and mapping through the resault which is RxList<Product> bindstream with query result.

here is my code:

Obx(() {
  return GridView.count(
    scrollDirection: Axis.vertical,
    primary: false,
    crossAxisCount:
        controller.filteredProducts.isEmpty ? 1 : 4,
    childAspectRatio: (1.9 / 1),
    children: controller.filteredProducts.isEmpty
        ? const [
            Center(
              child: Text(
                  "No Products! Try starting to type name of Product"),
            ),
          ]
        : controller.filteredProducts
            .map(
              (p) => _item(
                title: p.name,
                price: p.getPrice().toString(),
                item: p.stockCount().toString(),
              ),
            )
            .toList(),
  );
}),

in the above code there is no space to control index of list to solve the the error

RangeError (index): Invalid value: Valid value range is empty: 0

in the background, I'm trying to update list of products upon searchfield instantly by starting to type the name. When I print the list of filteredProducts it shows the right results but in the view it shows this error when I start typing the product name.

Poula Adel
  • 609
  • 1
  • 10
  • 33

1 Answers1

0

You can try it in you background task:

final sendPort =
        IsolateNameServer.lookupPortByName("notificationUpdatePort");
if (sendPort != null) {
  // The port might be null if the main isolate is not running.
  sendPort.send(['any', 'data', 'you', 'want']);
}

And to update your list, you can do like that:

var port = ReceivePort();
IsolateNameServer.registerPortWithName(
      port.sendPort, "notificationUpdatePort");
port.listen((dynamic data) async {
 // Here to update your list
});