Why am I explaining all of this? Why do I need to understand this when starting with Flutter?

The answer to this lies in the standard code that is generated when you create a new Flutter app. In main.dart , you’ll find two classes: MyHomePage and _MyHomePageState . MyHomePage extends StatefulWidget (Line #1) and _MyHomePageState extends State class (Line #7 ). This is shown in the code snippet below:

Something commonly misunderstood by people new to Flutter, or to statically typed languages in general, is the following line:

As you can see, we want to create a _MyHomePageState class that extends State . If you look carefully, you’ll see that the State class has a generic type <T>, and so we’re going to want to specify what Type we’re going to be using in place of T for our new _MyHomePageState class. When creating a State we need to define what it is the State of. We do this by defining T, which in this case is going to be our StatefulWidget , MyHomePage .

If you were to check the source code, this is what you’d find:

Class declaration for " State"

Since the source code needs to be reusable anytime we need to create a State for a StatefulWidget , it uses a generic type <T>.

Simply put, we’re telling Flutter to create a class _MyHomePageState which will borrow some functionality from State . Since we need to make sure this State is only responsible for a single StatefulWidget , we type it by adding that Widget’s type in <> brackets (here we use MyHomePage ) . By doing so, we tie this instance of State to the MyHomePage class.

This process is exactly what we did to our Slot class earlier.