Skip to content

Widget watching on Computed is sometimes not rebuiltΒ #18

@Delgan

Description

@Delgan

Hey @letsar!

So, I was doing some tests using your excellent package, and I noticed a very weird bug. Basically, I have one widget watching a Computed reference which itself is based on a State reference. Each time the button is pressed, the state is updated and the other reference is re-computed. So far so good.

However, the widget is not systematically rebuilt. It only works half the time, randomly. Yet, the value returned by the Computed is definitely different!

Peek.2021-05-30.16-21.mp4
import 'package:binder/binder.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

void main() => runApp(BinderScope(child: AppView()));

class AppView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          RaisedButton(
            child: Text("Press me"),
            onPressed: () => context.use(testLogicRef).click(),
          ),
          Builder(
            builder: (context) {
              final val = context.watch(computedValRef);
              print("Rebuilt with: $val");
              return Text("$val");
            },
          )
        ],
      ),
    );
  }
}

final testLogicRef = LogicRef((scope) => TestLogic(scope));

final testStateRef = StateRef(0);

final computedValRef = Computed<int>((watch) {
  final val = watch(testStateRef);
  final computedVal = DateTime.now().millisecondsSinceEpoch;
  print("Computed value: $computedVal");
  return computedVal;
});

class TestLogic with Logic {
  const TestLogic(this.scope);

  @override
  final Scope scope;

  void click() => update(testStateRef, (int val) => val + 1);
}

It drives me crazy. Am I doing something wrong?

Also, note that when I press the button once, the Computed is called twice or thrice. Is that expected?

I'm using Binder 0.4.0 with Flutter 2.0.6 and Dart 2.12.3.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions