3
votes

Could some please explain what is wrong?

I try to get an item by key from HashMap:

If I do it that way:

var stringProvider = providersHolder[String] I get an error:

NoSuchMethodError: Class 'Module' has no instance getter 'String'.

If I do it next way:

var stringProvider = providersHolder[providersHolder.keys.last] where providersHolder.keys.last == String

I get success.

Here is a full listing:

class Module {
  HashMap providersHolder = new HashMap<Type, ItemCreator>();

  void addProvider<T, M extends Module>(ItemCreator<T, M> itemCreator) {
     providersHolder[T] = itemCreator;
  }
}

class Test {
    Test() {
      var module = Module();
      module.addProvider((module) => new Ticker());
      module.addProvider((module) => "hello");
      var ticketProvider = module.providersHolder[Ticker]; //success
      var stringProvider = module.providersHolder[String]; //error
    } 
}

Here is a watcher:

watcher

1
Could you provide more details? Your code works for me. I defined ItemCreator as typedef T ItemCreator<T, M>(Module); which may differ from yours. Is this a debugger bug? - Richard Heap
@RichardHeap, I appreciate that you studied the matter in detail and even run the code. So far I can reproduce it only in the debugger. So, I guess your assumption that it's a debugger issue is right. - Eldar Miensutov

1 Answers

0
votes

Could you provide more details on how ItemCreator was defined? This will help us replicate the behavior you've encountered. If you're still having the same errors, you can also run flutter doctor -v to check for any errors or warnings.