Skip to content

Instantly share code, notes, and snippets.

@Tokenyet
Last active May 13, 2019 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tokenyet/5e9bb507096819b4da69c551efd0ef8f to your computer and use it in GitHub Desktop.
Save Tokenyet/5e9bb507096819b4da69c551efd0ef8f to your computer and use it in GitHub Desktop.
Error about generic in flutter
typedef bool GeneticFunction<T>(T geneticData, String key);
class CoolType {}
class Component<T> extends StatefulWidget {
Component({this.data, this.geneticFunc});
final T data;
final GeneticFunction<T> geneticFunc;
void test()
{
var testFunc = geneticFunc;
testFunc(data, "123");
}
@override
_ComponentState<T> createState() {
return _ComponentState<T>();
}
}
// Don't mind SingleTickerProviderStateMixin, just simulate my own filter scenario
class _ComponentState<T> extends State<Component> with SingleTickerProviderStateMixin
{
@override
void initState() {
print("test one");
var a = widget.geneticFunc;
print("test two");
super.initState();
}
@override
Widget build(BuildContext context) {
return Container(width: 20, height: 20,);
}
}
// Step 2. Add following widget to build the view, If not build the view, initState will not be called
Component<CoolType>(
geneticFunc: (CoolType a, String key){
return false;
},
)
// Step3. See the errors.
I/flutter ( 4677): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 4677): The following assertion was thrown building NotificationListener<KeepAliveNotification>:
I/flutter ( 4677): type '(CoolType, String) => bool' is not a subtype of type '(dynamic, String) => bool'
I/flutter ( 4677):
I/flutter ( 4677): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter ( 4677): more information in this error message to help you determine and fix the underlying cause.
I/flutter ( 4677): In either case, please report this assertion by filing a bug on GitHub:
I/flutter ( 4677): https://github.com/flutter/flutter/issues/new?template=BUG.md
I/flutter ( 4677):
I/flutter ( 4677): When the exception was thrown, this was the stack:
I/flutter ( 4677): #0 _ComponentState.initState
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment