How to fully dump / print variable to console in the Dart language?

后端 未结 8 1171
星月不相逢
星月不相逢 2021-01-03 20:37

Hey there I am searching for a function which is printing a dynamic variable as completely as possible to the console in Dart language.

In PHP for i

相关标签:
8条回答
  • 2021-01-03 21:04

    There is no built in function that generates such an output.

    print(variable) prints variable.toString() and Instance of 'FooBarObject' is the default implementation. You can override it in custom classes and print something different.

    You can use reflection (https://www.dartlang.org/articles/libraries/reflection-with-mirrors) to build a function yourself that investigates all kinds of properties of an instance and prints it the way you want. There is almost no limitation of what you can do and for debugging purposes it's definitely a fine option.

    For production web application it should be avoided because it limits tree-shaking seriously and will cause the build output size to increase notable. Flutter (mobile) doesn't support reflection at all.

    You can also use one of the JSON serialization packages, that make it easy to add serialization to custom classes and then print the serialized value. For example

    • https://pub.dartlang.org/packages/dson

    I think there are others, but I don't know about (dis)advantages, because I usually roll my own using https://pub.dartlang.org/packages/source_gen

    0 讨论(0)
  • 2021-01-03 21:09

    dart:developer library has inspect function that allows debuggers to open an inspector on the object.

    To use it add:

    import 'dart:developer';
    

    And the you can see your inspected variable/object in console with:

    inspect(myVar);
    
    0 讨论(0)
提交回复
热议问题