Skip to content

feat: bind(context) #30

@passsy

Description

@passsy

Coming from package:provider I'm testing lite_ref and context_plus. All get the job done.

context_plus has some interesting (syntactic) features, I'd love to see in lite_ref, too.

bind

class ControlPageController extends BeaconController {
  static final ref = Ref<ControlPageController>();

  late final list = B.list<String>(['a', 'b', 'c']);
}
  @override
  Widget build(BuildContext context) {
    // bind ControlPageController to context, accessible via ControlPageController.ref for all children
    ControlPageController.ref.bind(context, () => ControlPageController());

    // directly available, without Builder()
    final controller = ControlPageController.ref.of(context);
    print(controller); // directly accessible

    return SizedBox();
  }

With lite_ref, passing anything down to the child widgets requires wrapping with LiteRefScope with overrides and a Builder to access it directly

  @override
  Widget build(BuildContext context) {
    return LiteRefScope(
      overrides: {
        // provides ControlPageController to all child widgets
        ControlPageController.ref..overrideWith((ctx) => ControlPageController()),
      },
      child: Builder(
        builder: (context) {
          // requires new context to access the controller
          final controller = ControlPageController.ref.of(context);
          print(controller);

          return SizedBox();
        }
      ),
    );
  }

The syntax it is not a dealbreaker. But it is quite verbose and adds two levels of nesting.
That I have to define overrides feels off. It might be the only time I provide a value to child widgets, not overriding anything.

Would this bind API be possible for lite_ref, too?

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