0

I am setting the linting rules for my flutter project and cannot remove the use_build_context_synchronously warning. I am using GetX and flash in my project. The warning happens in a controller with the following code, more specifically, in the place where showFlash is used.

Future<List<Item>> getItems(BuildContext context) async {
    List<Item> items = await httpService.listItems();
    if (items.isEmpty) {
      showFlash(
        context: context,
        ...
      );
    }
    return items;
  }

In the documentation, it mentions that we can use mounted in StatefulWidget (https://dart-lang.github.io/linter/lints/use_build_context_synchronously.html), but I am currently in GetController of GetX. Can anyone please help?

I find a similar question in StackOverflow Flutter - How to use mounted in GetX, but applying the solutions does not resolve the warning.

Cheers

Ben
  • 957
  • 1
  • 11
  • 37

1 Answers1

0

Not sure if it's bad practice or if it even works, but I suppose you could use Get.context. then you also don't need to pass it as parameter. like:

Future<List<Item>> getItems() async {
  List<Item> items = await httpService.listItems();
  if (items.isEmpty) {
    showFlash(
      context: Get.context!,
      ...
    );
  }
  return items;
}
Ivo
  • 18,659
  • 2
  • 23
  • 35