gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
Flutter how to pass class to function? Not instance but class itself
- User
Flutter how to pass class to function? Not instance but class itself
- Assistant
To pass a class to a function in Flutter, you can simply specify the class name as a parameter type. Here's an example:
codeclass Person { String name; int age; Person(this.name, this.age); } void printClassName(Type className) { print(className.toString()); } void main() { printClassName(Person); // outputs "Person" }In this example, the
printClassNamefunction takes aTypeparameter and prints the string representation of the parameter. To pass thePersonclass to the function, we simply passPersonas an argument.Note that when you pass a class type to a function, you cannot access its instance variables or call its instance methods directly. To do that, you need to create an instance of the class first.