call back in flutter
77
The declaration of VoidCallback is
typedef void VoidCallback();
That is the type of functions that can be called with zero arguments and which does not return a useful value. That does not seem to be what you want. It's not entirely clear what you do want since the program isn't syntactically valid, but would this work for you:
class MyClass {
static doSomething(int i) { /* ... */ }
MyOtherClass myOtherClass = new MyOtherClass(doSomething);
}
class MyOtherClass {
final void Function(int) callback;
MyOtherClass(this.callback);
void callCallaback() { callback(5); }
}
Here we define the type of the callback field to be the type of functions that can be called with one integer argument and which returns no useful value. The doSomething method has that type, so it can be assigned to callback.
You could also use a typedef to name the function:
typedef Int2VoidFunc = void Function(int);
// or: typedef void Int2VoidFunc(int arg);
class MyOtherClass {
final Int2VoidFunc callback;
MyOtherClass(this.callback);
void callCallaback() { callback(5); }
}
The effect is exactly the same, it just allows you to use a shorter name for the function type, but that only really makes sense if you use it a lot.
Are there any code examples left?
New code examples in category Other
-
Other 2023-03-27 22:50:10 how to select the whole line in vscode with keyboard shortcut
-
Other 2022-03-27 22:45:24 income of a web developer
-
Other 2022-03-27 22:35:01 \pyrcc_main.py: File does not exist 'resources.qrc'
-
Other 2022-03-27 22:30:45 rick roll embed code
-
Other 2022-03-27 22:20:08 Circuit_04_Potentiometer
-
Other 2022-03-27 22:20:05 iterative power
-
Other 2022-03-27 22:15:11 flutter run all
-
Other 2022-03-27 22:10:05 when is karlson release
-
Other 2022-03-27 22:10:02 wp .htaccess example
-
Other 2022-03-27 22:00:08 bash pause in file read line by line