Getx: Dependencies created with a tag using Get.lazyPut throws an exception when using Get.find with the same tag

Created on 10 Sep 2020  ยท  3Comments  ยท  Source: jonataslaw/getx

This issue is entirely related to #569. Since I was not able to reopen #569, I'm creating a new issue instead.

My response to the comment of @roipeker :

Sorry @roipeker is my fault, I've just wanted to show a quick example, and I've used GetWidget as an _habit_. But no, this is not the reason of the problem. As I said, when you do Get.lazyPut(() => Controller(), tag: "tag"), the next call to Get.find(tag: "tag") will throw an exception saying The getter isInit was called on null. This is not beacause of GetWidget. And I think it is because of that:

S find<S>({String tag}) {
    final key = _getKey(S, tag);
    if (isRegistered<S>(tag: tag)) {
      if (_singl[key] == null) {
        if (tag == null) {
          throw 'Class "$S" is not register';
        } else {
          throw 'Class "$S" with tag "$tag" is not register';
        }
      }
      _initDependencies<S>(name: tag);
      return _singl[key].getDependency() as S;
    } else {
      if (!_factory.containsKey(key)) {
        // ignore: lines_longer_than_80_chars
        throw '"$S" not found. You need to call "Get.put($S())" or "Get.lazyPut(()=>$S())"';
      }

      GetConfig.log('Lazy instance "$S" created');

      var _value = put<S>(_factory[key].builder() as S); // THIS LINE HERE

      _initDependencies<S>(name: tag);

      if (GetConfig.smartManagement != SmartManagement.keepFactory &&
          !_factory[key].fenix) {
        _factory.remove(key);
      }

      return _value;
    }
  }

The dependency created using lazyPut with a tag is registered at the right time using put but without tag, so in _initDependencies:

bool _initDependencies<S>({String name}) {
    final key = _getKey(S, name);
    final isInit = _singl[key].isInit; // THIS LINE THROWS THE EXCEPTION
    if (!isInit) {
      _startController<S>(tag: name);
      _singl[key].isInit = true;
      if (GetConfig.smartManagement != SmartManagement.onlyBuilder) {
        _registerRouteInstance<S>(tag: name);
      }
    }
    return true;
  }

The generated key doesn't match any dependency and the array access return a null value...

I'm reopening the issue, so please @roipeker or @jonataslaw investigate in this. Sorry for the last time I've not well explained the problem because I was a bit busy and I wanted to write something quickly.

This is a part of the stack trace:

โ•โ•โ•ก EXCEPTION CAUGHT BY WIDGETS LIBRARY โ•žโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
The following NoSuchMethodError was thrown building Builder(dirty):
The getter 'isInit' was called on null.
Receiver: null
Tried calling: isInit
The relevant error-causing widget was:
  Navigator-[LabeledGlobalKey<NavigatorState>#a65be]

lib\โ€ฆ\chat\chat_page.dart:111
When the exception was thrown, this was the stack:
#0      Object.noSuchMethod  (dart:core-patch/object_patch.dart:51:5)
#1      GetInstance._initDependencies           package:get/โ€ฆ/instance/get_instance.dart:150
#2      GetInstance.find                                  package:get/โ€ฆ/instance/get_instance.dart:231
#3      Inst.find                                               package:get/โ€ฆ/instance/extension_instance.dart:54
#4      new ChatMessaging                           package:isintu_chat/โ€ฆ/widgets/chat_messaging_widget.dart:46
#5      ChatPage.build.<anonymous closure>.<anonymous closure>.<anonymous closure>    lib\โ€ฆ\chat\chat_page.dart:126
#6      GetPageRoute.buildPage 

_Originally posted by @na2axl in https://github.com/jonataslaw/getx/issues/569#issuecomment-690043158_

Waiting for customer response

Most helpful comment

The PR was just merged, @na2axl , please update and confirm the issue is solved.

All 3 comments

Sorry my misunderstanding yesterday @na2axl. You were totally right with the regression issue, and the missing tag. I just pushed a PR. Thanks for letting us know.

The PR was just merged, @na2axl , please update and confirm the issue is solved.

Hello @roipeker, sorry for the delay. I can confirm that everything works as expected now ๐Ÿ‘๐Ÿพ. Thanks for that, I will close the issue now.

Was this page helpful?
0 / 5 - 0 ratings