letsar/binder

The method 'write' was called on null

erf opened this issue · 3 comments

erf commented

I made a simple example and i fail to write a state on a button press.

Did i do something silly?

flutter --version

Flutter 2.1.0-12.2.pre • channel beta • https://github.com/flutter/flutter.git
Framework • revision 5bedb7b1d5 (10 days ago) • 2021-03-17 17:06:30 -0700
Engine • revision 711ab3fda0
Tools • Dart 2.13.0 (build 2.13.0-116.0.dev)

I use binder version ^0.4.0

And i used the following environment (default from flutter create):

environment:
  sdk: ">=2.7.0 <3.0.0"

Example:

import 'package:flutter/material.dart';
import 'package:binder/binder.dart';

final testRef = StateRef(100);

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

class TestLogic with Logic {
  TestLogic(Scope scope);

  @override
  Scope scope;

  void writeState(int val) {
    write(testRef, val);
  }
}

void main() {
  runApp(BinderScope(child: MyApp()));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Binder test'),
      ),
      body: Center(
        child: Column(
          children: [
            Text(context.watch(testRef).toString()),
            TextButton(
              child: Text('Test'),
              onPressed: () {
                context.use(testLogicViewRef).writeState(999);
              },
            ),
          ],
        ),
      ),
    );
  }
}

I get the following error:

Restarted application in 327ms.

�[38;5;248m════════ Exception caught by gesture ═══════════════════════════════════════════�[39;49m
�[38;5;244mThe following NoSuchMethodError was thrown while handling a gesture:�[39;49m
The method 'write' was called on null.
Receiver: null
Tried calling: write<int>(Instance of 'StateRef<int>', 999, null)

�[38;5;244mWhen the exception was thrown, this was the stack�[39;49m
�[38;5;244m#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)�[39;49m
�[38;5;244m#1      Logic.write�[39;49m
�[38;5;248m#2      TestLogic.writeState�[39;49m
�[38;5;248m#3      MyHomePage.build.<anonymous closure>�[39;49m
�[38;5;244m#4      _InkResponseState._handleTap�[39;49m
�[38;5;244m...�[39;49m
�[38;5;244mHandler: "onTap"�[39;49m
�[38;5;244mRecognizer: TapGestureRecognizer#e2765�[39;49m
    �[38;5;244mdebugOwner: GestureDetector�[39;49m
    �[38;5;244mstate: possible�[39;49m
    �[38;5;244mwon arena�[39;49m
    �[38;5;244mfinalPosition: Offset(192.0, 106.0)�[39;49m
    �[38;5;244mfinalLocalPosition: Offset(36.5, 7.0)�[39;49m
    �[38;5;244mbutton: 1�[39;49m
    �[38;5;244msent tap down�[39;49m
�[38;5;248m════════════════════════════════════════════════════════════════════════════════�[39;49m

Oh I see, the scope is not affected. Replace TestLogic(Scope scope); with TestLogic(this.scope); and it should work.
If you're using vscode, you can add this extension and use the lref snippet. It will correctly create the Logic.

erf commented

Ah. My bad. I actually had installed you plugin (which is great). But i did not use it that time. Was just testing out some stuff.

No worries 😉