s0nerik / context_plus

Convenient BuildContext-based value propagation and observing. Seamlessly integrates with Flutter's built-in observability primitives.
MIT License
32 stars 4 forks source link

Rebuilds with same value #3

Closed jinyus closed 8 months ago

jinyus commented 8 months ago

I made rainbench to test the throughput of different libs and I am trying to add context_watch but it's not working. A mutation triggers a rebuild but the ValueNotifier has the same value. It works fine with ValueListenableBuilder.

Here is the code: https://github.com/jinyus/rainbench/blob/92f1742febd15cdecda5542768a1824098fe2a2f/lib/rain/value_notifier.dart#L47

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

  @override
  Widget build(BuildContext context) {
    final screenWidth = MediaQuery.of(context).size.width;
    return Stack(
      children: [
        // Cloud at top
        Clouds(screenWidth: screenWidth),
        const Progress(),

        // Raining drops based on beacon position
        for (int i = 0; i < rainDropCount.peek(); i++)
          Builder(
            builder: (context) {
              final startingLeftOffset = (screenWidth - totalRowWidth) / 2;
              final row = i ~/ columns;
              final col = i % columns;
              final val = cw.ValueListenableContextWatchExtension(
                valueNotifierObservable.observable,
              ).watch(context);

              return Positioned(
                left: startingLeftOffset + col * (dropWidth + dropSpacing),
                top: initialTopOffset +
                    row * dropSpacing +
                    200.0 * (1 + (val * .1)),
                child: const Icon(
                  Icons.water_drop,
                  size: dropWidth,
                  color: Colors.blue,
                ),
              );
            },
          ),
      ],
    );
  }
}
s0nerik commented 8 months ago

Looks like you forgot to wrap the MaterialApp in a ContextWatchRoot. With context_watch v2+ you'd receive an error in this case.

s0nerik commented 8 months ago

BTW, I'd recommend trying context_watch v2.0.0-dev.4 or later for the benchmark if possible as it should have a better performance. The only reason v2 is not released yet is that it requires a beta version of Flutter at the moment.

jinyus commented 8 months ago

Looks like you forgot to wrap the MaterialApp in a ContextWatchRoot. With context_watch v2+ you'd receive an error in this case.

Thanks, this fixed it, I'll use v2