Info: 'runZoned' is deprecated and shouldn't be used. This will be removed in v9.0.0. Use Bloc.Bloc.transformer instead

Viewed 247

I am getting this issue while trying to run my code on DartPad.

'runZoned' is deprecated and shouldn't be used. This will be removed in v9.0.0. Use Bloc.Bloc.transformer instead...

What is the correct way to replace it?

code:

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

void main() {
  BlocOverrides.runZoned(
    () => runApp(const App()),
    blocObserver: AppBlocObserver(),
  );
}

/// Custom [BlocObserver] that observes all bloc and cubit state changes.
class AppBlocObserver extends BlocObserver {
  @override
  void onChange(BlocBase bloc, Change change) {
    super.onChange(bloc, change);
    if (bloc is Cubit) print(change);
  }

  @override
  void onTransition(Bloc bloc, Transition transition) {
    super.onTransition(bloc, transition);
    print(transition);
  }
}
2 Answers

The above code, while appearing harmless, can actually lead to many difficult to track bugs. Due to the use of the runZoned, the transition to the BlocOverrides API led to the discovery of several bugs/limitations in Flutter

https://github.com/flutter/flutter/issues/96939

To fix this, use the following way of creating observers and transformers as given by the Bloc team

void main() {
  Bloc.observer = AppBlocObserver();
  Bloc.transformer = customEventTransformer();

  // ...
}

You can read more about bloc on their official website.

Solution is :

void main() {
  Bloc.observer = AppBlocObserver();
  runApp(const App());
}
Related