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

后端 未结 8 1166
星月不相逢
星月不相逢 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 20:58

    Instance of 'FooBarObject' This happens when you try to directly do print(object);

    Even if you use convert package from flutter, it would throw Uncaught Error: Converting object to an encodable object failed:

    To resolve this issue, first, make sure your model class or data class contains fromJson and toJson methods.

    If you do so, your model class would look like this.

    class User {
      String name;
      String gender;
      int age;
    
      User({this.name, this.gender, this.age});
    
      User.fromJson(Map json) {
        name = json['name'];
        gender = json['gender'];
        age = json['age'];
      }
    
      Map toJson() {
        final Map data = new Map();
        data['name'] = this.name;
        data['gender'] = this.gender;
        data['age'] = this.age;
        return data;
      }
    }
    

    And to print this as json import convert package

    import 'dart:convert';
    

    And call json.encode

    //after initializing the user object
     print('value is--> ' + json.encode(user);
    

    Which will print the data as

    value is--> {"name": "Vignesh","gender": "Male","age": 25}
    

提交回复
热议问题