Skip to content

Instantly share code, notes, and snippets.

@gladson
Last active October 31, 2021 21:13
Show Gist options
  • Save gladson/62c0c67d04e76011321edc65798cfd79 to your computer and use it in GitHub Desktop.
Save gladson/62c0c67d04e76011321edc65798cfd79 to your computer and use it in GitHub Desktop.
Dart & Flutter - Log Debugger (Break Point programaticamente) & Timeline (Análise de performance)

import 'dart:developer' as developer;

import 'dart:developer' as developer;

Criar logs com mensagem personalizada

sem o uso do pacote Logger

press: () async {
    try {
      int.parse('abc');
      controller.doLogin();
    } on Exception catch (error, stackTrace) {
      developer.log(
        'Login',
        name: 'APP',
        error: error.toString(),
        stackTrace: stackTrace,
        time: DateTime.now(),
      );
    }
  }

Debugar através do código

press: () async {
    try {
      // Debugar sem condicional
      developer.debugger();
      // Debugar com condicional
      developer.debugger(
         when: controller.cpf.text == '99999999999',
      );
      controller.doLogin();
    } on Exception catch (error, stackTrace) {
      developer.log(
        'Login',
        name: 'APP',
        error: error.toString(),
        stackTrace: stackTrace,
        time: DateTime.now(),
      );
    }
  }

Inspecionar elemento

press: () {
    try {
      developer.inspect(this);
      developer.inspect(controller.cpf.text);
      controller.doLogin();
    } on Exception catch (error, stackTrace) {
      developer.log(
        'Login',
        name: 'APP',
        error: error.toString(),
        stackTrace: stackTrace,
        time: DateTime.now(),
      );
    }
  }

Resultado:

<inspected variable>
String:"99999999999"

<inspected variable>
_formKey:LabeledGlobalKey ([LabeledGlobalKey<FormState>#e0edb])
_location:_Location (AuthView:app_pages.dart)
_scaffoldKey:LabeledGlobalKey ([LabeledGlobalKey<ScaffoldState>#33ff0])
key:null
tag:null

Calcular o tempo de execução não assincrona

press: () {
    try {
      // Calcular o tempo de execução assincrona - inicio
      developer.Timeline.startSync('login_timeline');
      controller.doLogin();
    } on Exception catch (error, stackTrace) {
      developer.log(
        'Login',
        name: 'APP',
        error: error.toString(),
        stackTrace: stackTrace,
        time: DateTime.now(),
      );
    }
    // Calcular o tempo de execução assincrona - fim
    developer.Timeline.finishSync();
  }

Ativar o "Open DevTools in Web Browser": image

E na pagina DevTools pesquisar no "Timeline Events" o nome 'login_timeline': image

Calcular o tempo de execução assincrona

press: () async {
      final timelineTask = developer.TimelineTask();
      timelineTask.start('login_timeline_async');
      try {
        await Future.delayed(const Duration(seconds: 2));
        developer.inspect('teste');
      } on Exception catch (error, stackTrace) {
        developer.log(
          'Login',
          name: 'Abastece Mais',
          error: error.toString(),
          stackTrace: stackTrace,
          time: DateTime.now(),
        );
      }
      timelineTask.finish();
    },

Resultado: image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment