MainBlocEvent class abstract
A base class for all Bloc events in the application.
This abstract class serves as a foundation for creating specific event classes that can be used with Blocs. All custom event classes should extend this class.
The const constructor allows for efficient instantiation of event objects.
Example with regular class:
class MySpecificEvent extends MainBlocEvent {
final String someData;
const MySpecificEvent(this.someData);
}
// Using the event
bloc.add(MySpecificEvent('Some data'));
Example with Freezed 3.0.0:
import 'package:freezed_annotation/freezed_annotation.dart';
part 'my_event.freezed.dart';
@freezed
sealed class MyEvent extends MainBlocEvent with _$MyEvent {
const MyEvent._(); // Required private constructor
const factory MyEvent.started() = Started;
const factory MyEvent.dataLoaded(String data) = DataLoaded;
}
// Using the event with pattern matching
void handleEvent(MyEvent event) {
final result = switch (event) {
Started() => 'Starting...',
DataLoaded(:final data) => 'Data: $data',
};
}
Note: When using with Freezed 3.0.0+:
- Use
sealedkeyword for pattern matching with fixed number of subtypes - Use
abstractkeyword if the class can be extended/implemented - Extend MainBlocEvent instead of implementing it
- Always include a private constructor when using extends/with
- Use Dart's built-in pattern matching instead of .when/.map methods
- Implementers
Constructors
- MainBlocEvent()
-
const
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited