fluttervisual-studio-codeflutter-getx

Why my variable on page doesn't update after hot restart, but works on first launch?


I got Controller like this

class UiKitTableContoller extends GetxController {
final TreeViewController _controller = TreeViewController();
TreeViewController get treeController => _controller;

final RxBool _isSuccess = false.obs;
RxBool get isSuccess => _isSuccess;

void getData(List<TableRow> headRows) async {
  _isSuccess.value = false;
  await Future.delayed(const Duration(seconds: 2));

  ...

  _isSuccess.value = true;
  update();
}

and page like this

@override
Widget build(BuildContext context) {
uiKitTableCtrl.getData(children);
return GetBuilder<UiKitTableContoller>(
    init: uiKitTableCtrl,
    builder: (_) {
      return Scaffold(
        body: !uiKitTableCtrl.isSuccess.value
            ? const Center(child: CircularProgressIndicator())
            : ListTreeView(...)
  }
}

I have a check on page, if success then I show ListTreeView(...), else CircularProgressIndicator(). So the problem is my data is loading only on first time and it shows ListTreeView(...), but when I do hot reload it stucks on CircularProgressIndicator().

But _isSuccess updates inside controller even after hot reload and shows true, but on page it stays false.

Any suggestions?


Solution

  • Here's the answer to my question. My problem was in Obx(). I had to put it there:

    @override
    Widget build(BuildContext context) {
    uiKitTableCtrl.getData(children);
    return GetBuilder<UiKitTableContoller>(
        init: uiKitTableCtrl,
        builder: (_) {
          return Scaffold(
       
    

    body: Obx( () => !uiKitTableCtrl.isSuccess.value ? const Center(child: CircularProgressIndicator()) : ListTreeView(...) );

      }
    }
    

    Hope it will help someone :)