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

后端 未结 8 1173
星月不相逢
星月不相逢 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:02

    Simple little trick does the job

    void printObject(Object object) {
      // Encode your object and then decode your object to Map variable
      Map jsonMapped = json.decode(json.encode(object)); 
    
      // Using JsonEncoder for spacing
      JsonEncoder encoder = new JsonEncoder.withIndent('  '); 
    
      // encode it to string
      String prettyPrint = encoder.convert(jsonMapped); 
    
      // print or debugPrint your object
      debugPrint(prettyPrint); 
    }
    
    // To use simply pass your object to it
    printObject(yourObject); 
    

提交回复
热议问题