dvRunApplication function

Future<void> dvRunApplication(
  1. List<String> args, {
  2. required Widget root(),
  3. List<DVBootStep> boot = const [],
  4. FutureOr<void> launch(
    1. List<String>
    )?,
})

Framework entry used by the generated application facade.

Implementation

Future<void> dvRunApplication(
  List<String> args, {
  required Widget Function() root,
  List<DVBootStep> boot = const [],
  FutureOr<void> Function(List<String>)? launch,
}) async {
  if (_running) {
    DV.log.warn(
      'DV.run was already called in this isolate.',
      code: 'DV-RUN-002',
    );
    return;
  }
  _running = true;
  _args = List.unmodifiable(args);
  final completed = Completer<void>();
  runZonedGuarded(
    () async {
      try {
        WidgetsFlutterBinding.ensureInitialized();
        DV.lifecycle.setApp(.initializing);
        if (launch != null) await launch(args);
        DV.lifecycle.setApp(.booting);
        // The same runner a server, a worker or a command boots through;
        // only the entry differs.
        await dvRunBoot(boot, DVBoot(entry: const DVEntry.app(), args: _args));
        final previousFlutterError = FlutterError.onError;
        FlutterError.onError = (details) {
          DV.log.error(
            'Flutter error.',
            error: details.exception,
            stackTrace: details.stack,
          );
          previousFlutterError?.call(details);
        };
        dvStartAppLifecycleBridge();
        runApp(root());
        WidgetsBinding.instance.addPostFrameCallback((_) {
          DV.lifecycle.setApp(.ready);
        });
        completed.complete();
      } catch (error, stack) {
        DV.lifecycle.setApp(.failed);
        DV.log.error(
          'Application boot failed.',
          error: error,
          stackTrace: stack,
        );
        runApp(
          MaterialApp(
            theme: dartvelDefaultTheme(.light),
            darkTheme: dartvelDefaultTheme(.dark),
            home: DVBox(DVText('Unable to start the application.')),
          ),
        );
        completed.completeError(error, stack);
      }
    },
    (error, stack) {
      DV.log.error(
        'Uncaught application error.',
        error: error,
        stackTrace: stack,
      );
      if (!completed.isCompleted) completed.completeError(error, stack);
    },
  );
  await completed.future;
}