Last active
December 11, 2019 13:41
-
-
Save itsJoKr/d7ba98df97906bbd768ea13195580776 to your computer and use it in GitHub Desktop.
Dart generics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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