8

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);
  }
}
My Car
  • 4,198
  • 5
  • 17
  • 50
Sweta Jain
  • 3,248
  • 6
  • 30
  • 50

3 Answers3

20

Solution is :

void main() {
  Bloc.observer = AppBlocObserver();
  runApp(const App());
}
Sweta Jain
  • 3,248
  • 6
  • 30
  • 50
2

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.

austin
  • 517
  • 5
  • 16
0

For me this did the trick:

before:

BlocOverrides.runZoned( () {
  runApp(
    BlocProvider(
      create: (_) => AppBloc(
        screen: AppScreen.home,
      )..add(const AppStarted(screen: AppScreen.home)),
      child: const App(),
    ),
  );
},
blocObserver: SimpleBlocObserver(),

);

after:

Bloc.observer = SimpleBlocObserver();
runApp(
  BlocProvider(
    create: (_) => AppBloc(
      screen: AppScreen.home,
    )..add(const AppStarted(screen: AppScreen.home)),
    child: const App(),
  ),
);
midi
  • 3,128
  • 5
  • 30
  • 47