Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: get full stacktrace when exception is thrown?

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.

like image 310
stacktrace2234 Avatar asked Jul 21 '26 09:07

stacktrace2234


1 Answers

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()}]");
}
like image 193
DJafari Avatar answered Jul 22 '26 23:07

DJafari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!