I want to call a function that would return another function to be used as onChange event handler for a Textfield input.
I am trying the code below, but for some reason the emulator building process just stuck, no error showed and emulation not working.
Function testing(Counter counter) {
var somefunction = (String s) {
counter.increment();
};
return somefunction;
}
//widget class
@override
Widget build(BuildContext context) {
final counter = Provider.of<Counter>(context);
return TextField(
onChanged: testing(counter),
decoration: InputDecoration(labelText: _placeholder),
);
}
Here is an example of function returning another function as result
bool Function(String) testing(Counter counter) {
var somefunction = (String s) {
counter.increment();
};
return somefunction;
}
also this statement could be simplified to
bool Function(String) testing(Counter counter) => (String s) {
counter.increment();
};
For readability you can define a typedef. Probably most used one is:
typedef WidgetBuilder = Widget Function(BuildContext context);
So original example code may be converted to something like
typedef StringTester = bool Function(String);
StringTester testing(Counter counter) => (String s) {
counter.increment();
};
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments