Create Blocs authored by Nicholas Sarian's avatar Nicholas Sarian
# Blocs
There are two primary identifers in dart, which are `final` and `late`. Variables defined with the `final` keyword are constants and not expected to change once they receive a variable. Variables defined with the `late` keyword are initially created but initialized. Late variables are expected to be assigned a variable sometime after their declaration before they are first used. Examples of this are:
```dart
late CreatePlayerRequest data;
final PlayerRepository playerRepository;
```
Every bloc has a constructor, which can take in arguments just like classes. Here's an example:
```dart
CreatePlayerBloc({
required this.playerRepository
}) : super (CreatePlayerInitial())
```
In this example, the constructor requires a playerRepository be passed into it. When the constructor is finished, the constructor of the parent (Bloc) is called with the CreatePlayerInitial() state, which sets the current state as CreatePlayerInitial().
After the constructor, there is an event listener that listens for an event:
```dart
on<SendCreatePlayerEvent>((event, emit) async {
// code
}
```
In the above code, the bloc will be listening for a SendCreatePlayerEvent to be sent. Once the Bloc receives the SendCreatePlayerEvent, the asynchronous function will be called with an event object of type SendCreatePlayerEvent and the emit function, which is used to emit new states. The event object contains all of the information that was sent in the event
# Emitting a New State within Bloc
To emit a new state in Bloc, call the emit function with the new state. Example:
```dart
emit(CreatePlayerLoading());
```
In the example above, the CreatePlayerLoading state is emitted. This state will be used later in widgets, where you can check what the current state of the page is.
\ No newline at end of file