Repository: felangel/bloc (dart)

Issue:
refactor: `BlocObserver` could have a const constructor
**Description**

`BlocObserver` could have a const constructor to allow classes that extend it to have a const constructor too.

Pull request description:
<!--
  Thanks for contributing!

  Provide a description of your changes below and a general summary in the title

  Please look at the following checklist to ensure that your PR can be accepted quickly:
-->

## Status

**READY**

## Breaking Changes

NO

## Description

<!--- Describe your changes in detail -->

Fixes https://github.com/felangel/bloc/issues/3703 by adding a const constructor to `BlocObserver`

## Type of Change

<!--- Put an `x` in all the boxes that apply: -->

- [ ] ✨ New feature (non-breaking change which adds functionality)
- [ ] 🛠️ Bug fix (non-breaking change which fixes an issue)
- [ ] ❌ Breaking change (fix or feature that would cause existing functionality to change)
- [x] 🧹 Code refactor
- [ ] ✅ Build configuration change
- [ ] 📝 Documentation
- [ ] 🗑️ Chore

Patch:
diff --git a/packages/bloc/example/main.dart b/packages/bloc/example/main.dart
index 2f6965ff52c..dc10f597014 100644
--- a/packages/bloc/example/main.dart
+++ b/packages/bloc/example/main.dart
@@ -3,6 +3,8 @@ import 'dart:async';
 import 'package:bloc/bloc.dart';
 
 class SimpleBlocObserver extends BlocObserver {
+  const SimpleBlocObserver();
+
   @override
   void onCreate(BlocBase bloc) {
     super.onCreate(bloc);
@@ -41,7 +43,7 @@ class SimpleBlocObserver extends BlocObserver {
 }
 
 void main() {
-  Bloc.observer = SimpleBlocObserver();
+  Bloc.observer = const SimpleBlocObserver();
   cubitMain();
   blocMain();
 }
diff --git a/packages/bloc/lib/src/bloc.dart b/packages/bloc/lib/src/bloc.dart
index ffcfa51d64d..57fc5f647b7 100644
--- a/packages/bloc/lib/src/bloc.dart
+++ b/packages/bloc/lib/src/bloc.dart
@@ -46,7 +46,7 @@ abstract class Bloc<Event, State> extends BlocBase<State>
   Bloc(State initialState) : super(initialState);
 
   /// The current [BlocObserver] instance.
-  static BlocObserver observer = _DefaultBlocObserver();
+  static BlocObserver observer = const _DefaultBlocObserver();
 
   /// The default [EventTransformer] used for all event handlers.
   /// By default all events are processed concurrently.
@@ -293,7 +293,9 @@ class _Handler {
   final Type type;
 }
 
-class _DefaultBlocObserver extends BlocObserver {}
+class _DefaultBlocObserver extends BlocObserver {
+  const _DefaultBlocObserver();
+}
 
 class _FlatMapStreamTransformer<T> extends StreamTransformerBase<Stream<T>, T> {
   const _FlatMapStreamTransformer();
diff --git a/packages/bloc/lib/src/bloc_observer.dart b/packages/bloc/lib/src/bloc_observer.dart
index 05689c44523..d8ed8447c12 100644
--- a/packages/bloc/lib/src/bloc_observer.dart
+++ b/packages/bloc/lib/src/bloc_observer.dart
@@ -1,8 +1,13 @@
 import 'package:bloc/bloc.dart';
 import 'package:meta/meta.dart';
 
+/// {@template bloc_observer}
 /// An interface for observing the behavior of [Bloc] instances.
+/// {@endtemplate}
 abstract class BlocObserver {
+  /// {@macro bloc_observer}
+  const BlocObserver();
+
   /// Called whenever a [Bloc] is instantiated.
   /// In many cases, a cubit may be lazily instantiated and
   /// [onCreate] can be used to observe exactly when the cubit


Test patch:
diff --git a/packages/bloc/test/bloc_observer_test.dart b/packages/bloc/test/bloc_observer_test.dart
index 70f644154d9..69329246aca 100644
--- a/packages/bloc/test/bloc_observer_test.dart
+++ b/packages/bloc/test/bloc_observer_test.dart
@@ -3,7 +3,9 @@ import 'package:test/test.dart';
 
 import 'blocs/blocs.dart';
 
-class DefaultBlocObserver extends BlocObserver {}
+class DefaultBlocObserver extends BlocObserver {
+  const DefaultBlocObserver();
+}
 
 void main() {
   final bloc = CounterBloc();
@@ -20,42 +22,42 @@ void main() {
     group('onCreate', () {
       test('does nothing by default', () {
         // ignore: invalid_use_of_protected_member
-        DefaultBlocObserver().onCreate(bloc);
+        const DefaultBlocObserver().onCreate(bloc);
       });
     });
 
     group('onEvent', () {
       test('does nothing by default', () {
         // ignore: invalid_use_of_protected_member
-        DefaultBlocObserver().onEvent(bloc, event);
+        const DefaultBlocObserver().onEvent(bloc, event);
       });
     });
 
     group('onChange', () {
       test('does nothing by default', () {
         // ignore: invalid_use_of_protected_member
-        DefaultBlocObserver().onChange(bloc, change);
+        const DefaultBlocObserver().onChange(bloc, change);
       });
     });
 
     group('onTransition', () {
       test('does nothing by default', () {
         // ignore: invalid_use_of_protected_member
-        DefaultBlocObserver().onTransition(bloc, transition);
+        const DefaultBlocObserver().onTransition(bloc, transition);
       });
     });
 
     group('onError', () {
       test('does nothing by default', () {
         // ignore: invalid_use_of_protected_member
-        DefaultBlocObserver().onError(bloc, error, stackTrace);
+        const DefaultBlocObserver().onError(bloc, error, stackTrace);
       });
     });
 
     group('onClose', () {
       test('does nothing by default', () {
         // ignore: invalid_use_of_protected_member
-        DefaultBlocObserver().onClose(bloc);
+        const DefaultBlocObserver().onClose(bloc);
       });
     });
   });