Skip to content

Instantly share code, notes, and snippets.

@itsJoKr
Last active December 11, 2019 13:41
Show Gist options
  • Save itsJoKr/d7ba98df97906bbd768ea13195580776 to your computer and use it in GitHub Desktop.
Save itsJoKr/d7ba98df97906bbd768ea13195580776 to your computer and use it in GitHub Desktop.
Dart generics
import 'package:flutter/material.dart';
typedef Widget FormWidgetBuilder<T>(T t);
class GenericShizzle<T> extends StatefulWidget {
final FormWidgetBuilder<T> builder;
const GenericShizzle({
this.builder,
});
@override
GenericShizzleState<T> createState() {
return GenericShizzleState<T>();
}
}
// Simplest example
// You need to add <T> at the end here: State<GenericShizzle<T>>
class GenericShizzleState<T> extends State<GenericShizzle> {
@override
Widget build(BuildContext context) {
return Text(
"my builder is of type:" + widget.builder.runtimeType.toString());
}
}
void main() {
runApp(
MaterialApp(
home: Scaffold(
body: Center(
child: GenericShizzle(
builder: (int i) => Text(i.toString()),
),
),
),
),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment