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

4

11

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);
  }
}
Bute answered 12/9, 2022 at 7:37 Comment(0)
B
28

Solution is :

void main() {
  Bloc.observer = AppBlocObserver();
  runApp(const App());
}
Bute answered 23/9, 2022 at 10:55 Comment(0)
K
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.

Knout answered 12/9, 2022 at 7:54 Comment(0)
A
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(),
  ),
);
Almire answered 3/1, 2023 at 21:19 Comment(0)
E
0

you can try this Solution :-

write this code in main app file => Bloc.observer = AppBlocObserver();

enter image description here

Ettaettari answered 12/11, 2023 at 18:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.