gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
Flutter call method by string. How?
- User
Flutter call method by string. How?
- Assistant
To call a method by string in Flutter, you can use reflection. Here's an example:
- Import 'dart:mirrors' package.
dartimport 'dart:mirrors';- Create your class with the method you want to call:
dartclass MyController { void greet() { print('Hello'); } }- Instantiate your class:
dartfinal myController = MyController();- Get the instance mirror of your class:
dartfinal instanceMirror = reflect(myController);- Use the 'invoke' method on the instance mirror passing in the name of the method you want to call as a symbol:
dartinstanceMirror.invoke(Symbol('greet'), []);This will call the 'greet' method on your 'MyController' class and print 'Hello' to the console.