states_rebuilder icon indicating copy to clipboard operation
states_rebuilder copied to clipboard

a state DependsOn async states throw when access to own stateAsync

Open dehypnosis opened this issue 2 years ago • 0 comments

At first.. I had tried a number of state management libraries like provider, getx, mobx, ... and hook based access in production. But none of them is as much simple and intuitive like state_rebuilder! I want to appreciate your really wonderful system and contribution. And hope this library be widely used by people soon.

And the issue is that when i use DependOn with states from injectFuture or injectStream.. when i access to stateAsync of dependent state, it throws

final a = RM.injectFuture(
  () async {
    await Future.delayed(Duration(seconds: 1));
    return true;
  },
);
final b = RM.injectFuture<bool>(
  () async {
    await a.initializeState();
    return a.state;
  },
  dependsOn: DependsOn({a}),
);

class DemoPage extends StatelessWidget {
  const DemoPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FutureBuilder(
        future: Future.value(b.initializeState()), // same result for b.stateAsync
        builder: (context, snapshot) {
          return Text('NOTHING');
        },
      ),
    );
  }
}

---
CONSOLE


flutter: \^[[33m[states_rebuilder::ERROR]:  : The state is waiting and it is not initialized yet<…>
flutter: \^[[33m[states_rebuilder::ERROR]:  : OTHERWISE, TRY DEFINING THE INITIAL STATE OR HANDLE THE WAITING STATUS<…>
flutter: \^[[33m[states_rebuilder::ERROR]:  : The state is waiting and it is not initialized yet<…>
flutter: \^[[33m[states_rebuilder::ERROR]:  : OTHERWISE, TRY DEFINING THE INITIAL STATE OR HANDLE THE WAITING STATUS<…>

Setting initialState for b make it work, but in practical, I have a problem to set dummy initialState for some cases..

So if I make a chained future to wait dependent states sequentially, It also make it work.

      body: FutureBuilder(
        future: a.stateAsync.then((value) => b.stateAsync),
        builder: (context, snapshot) {
          return Text(snapshot.hasData ? b.state.toString() : 'LOADING');
        },
      ),

But It is hard to track all dependency tree for every cases in large app. I wonder that constructor of b already made to wait for a, but it seems ignore that.. Or,, Will DependsOn directly access to a?

Thank you for reading.

dehypnosis avatar Feb 16 '23 02:02 dehypnosis