Flutter: get full stacktrace when exception is thrown?

1,101

second argument of catch is stackTrace

try {
  List<MyDto> dtoList = dtoFromJson(body);
  if (dtoList.isNotEmpty) {
    for (var i in dtoList) {
      await _insertIntoDB(i, db);
    }
  }
} catch (error, stack) {
  print(stack);
  throw Exception(
      "Problem while JSON decoding results. [error=${error.toString()}]");
}
Share:
1,101
stacktrace2234
Author by

stacktrace2234

Updated on December 31, 2022

Comments

  • stacktrace2234
    stacktrace2234 over 1 year

    I built an app, I have a couple of try-catch. The problem is that whenever I print out the exception, it prints out only the first line of the exception. For example when I remove all the try-catch and the exception is thrown it shows this:

    E/flutter (15478): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Null check operator used on a null value
    E/flutter (15478): #0      MyClass._insertIntoDB (package:test/util/my_class.dart:186:25)
    E/flutter (15478): <asynchronous suspension>
    E/flutter (15478): #1      MyClass.download(package:test/util/my_class.dart:62:11)
    E/flutter (15478): <asynchronous suspension>
    E/flutter (15478): #2      blabla.asaa(package:test/sync.dart:94:9)
    E/flutter (15478): <asynchronous suspension>
    E/flutter (15478): #3      tata.dodo(package:test/my_screen.dart:91:11)
    E/flutter (15478): <asynchronous suspension>
    E/flutter (15478): 
    

    Whenever this happens, I can see where the problem happened: MyClass._insertIntoDB (package:test/util/my_class.dart:186:25) <--

    But when I put the given code between a try-catch, I get back only the:

    E/flutter (15478): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Null check operator used on a null value

    message, what says what is the problem but it does not say where...

    This is where the exception handled first:

     try {
          List<MyDto> dtoList = dtoFromJson(body);
          if (dtoList.isNotEmpty) {
            for (var i in dtoList) {
              await _insertIntoDB(i, db);
            }
          }
        } catch (error) {
          throw Exception(
              "Problem while JSON decoding results. [error=${error.toString()}]");
        }
    

    Here the error is equals to: [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Null check operator used on a null value

    So I get back this error: "Problem while JSON decoding results. [error=[ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Null check operator used on a null value]"

    So the actual useful information that where the exception is thrown (what row in the file) is not shown... Is it possible to include the rest of the stacktrace?

    Please be attentive and see that I don't ask why the exception is thrown! (that is not why I don't include the insertIntoDb code).

    Thanks in advance.