runZonedGuarded in flutter
Asked Answered
B

1

10
// Copyright (c) 2021, Very Good Ventures
// https://verygood.ventures
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

import 'dart:async';
import 'dart:developer';

import 'package:bloc/bloc.dart';
import 'package:flutter/widgets.dart';

class AppBlocObserver extends BlocObserver {
  @override
  void onChange(BlocBase bloc, Change change) {
    super.onChange(bloc, change);
    log('onChange(${bloc.runtimeType}, $change)');
  }

  @override
  void onError(BlocBase bloc, Object error, StackTrace stackTrace) {
    log('onError(${bloc.runtimeType}, $error, $stackTrace)');
    super.onError(bloc, error, stackTrace);
  }
}

Future<void> bootstrap(FutureOr<Widget> Function() builder) async {
  Bloc.observer = AppBlocObserver();
  FlutterError.onError = (details) {
    log(details.exceptionAsString(), stackTrace: details.stack);
  };

  await runZonedGuarded(
    () async => runApp(await builder()),
    (error, stackTrace) => log(error.toString(), stackTrace: stackTrace),
  );
}

Can someone explain this code snippet, I am just confused with runZonedGuarded() and bootstrap(FutureOr Function() builder). This code generated by very_good CLI, which will create a starter boilerplate for flutter app.

Badtempered answered 11/11, 2021 at 19:44 Comment(0)
G
10

Future<void> bootstrap(FutureOr<Widget> Function() builder) async { ...

is defining a function called bootstrap. This function takes a parameter called builder that is a

  • function taking no parameters
  • returning a Future or a Widget

and it will execute asynchronously (builder may or may not execute asynchronously).

The Bloc.observer = AppBlocObserver(); is a deprecated way of observing what the bloc(s) of the application are doing. The AppBlocObserver is defining a bunch of callbacks that will be executed when "events" are fired, like error, and change.

With version >= 8.0 of the BLoC- library you should likely do something like below instead of the run zone guarded postend in the question, since they moved the bloc observer property.

runZonedGuarded(
  () => BlocOverrides.runZoned(
    () async => runApp(await builder()), 
    blocObserver: AppBlocObserver()
  ),
  (error, stackTrace) => log(error.toString(), stackTrace: stackTrace));

This 'builder' method that's potentially an async method - or not - is then going to be executed and the returning widget is going to be the parameter to runApp - witch is going to

Inflate the given widget and attach it to the screen.

That app is then going to be run in an error zone, where the interesting part is

The onError function is used both to handle asynchronous errors by overriding ZoneSpecification.handleUncaughtError in zoneSpecification, if any, and to handle errors thrown synchronously by the call to body.

Gae answered 10/2, 2022 at 2:49 Comment(1)
UPDATE: Bloc.observer was reintroduced in 8.1.0 and BlocOverrides was deprecated.Czarist

© 2022 - 2024 — McMap. All rights reserved.