appinioGmbH / flutter_packages

Dart and Flutter plugins/packages used and maintained by @appinioGmbH
193 stars 224 forks source link

[ BUG ] : Can't use controller in initstate post frame callback #211

Open caseycrogers opened 11 months ago

caseycrogers commented 11 months ago

Plugin name Appinio Swiper

Describe the bug Controllers are attached after initstate in a post frame callback. This means that if you try to use a controller in your post frame callback it throws because it runs before the controller attach logic runs.

To Reproduce Steps to reproduce the beh See example code: https://github.com/appinioGmbH/flutter_packages/blob/bd9f10ef6caf9994d3380af37b6ac9ceafc70eab/packages/appinio_swiper/example/lib/main.dart#L43 If you switch the future delayed with just a regular post frame callback it throws.

Additional Context The core problem here is that attach relies on position which in turn relies on build context. We should be able to attach the controller directly in init state, but then have position be a late variable or something.

Will take me a bit to get to this but in a week or so I can submit a PR myself to fix this.

stuartrapop commented 8 months ago

Any update ? I would like to use this to have make a custom back swipe.

caseycrogers commented 8 months ago

Any update ? I would like to use this to have make a custom back swipe.

I won't have any availability to work on this. Maybe the package maintainers will have time to tackle it?

In the meantime, here is a really silly hack that should work:

    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      // Double nested post frame callback so that this is a post-2-frames callback.
      WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
        // Listening to the controller in here should be safe.
      });
    });