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

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

    use await keyword with an object in async function void _registerUser() async { print(await form.result); }

    0 讨论(0)
  • 2021-01-03 20:57

    If you want to know more about an instance of any class in an android studio click on a class name before variable then press ctrl+b it will take you to the class file where this object belongs from there you can see user attributes and methods of this class.

    0 讨论(0)
  • 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<String, dynamic> json) {
        name = json['name'];
        gender = json['gender'];
        age = json['age'];
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        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}
    
    0 讨论(0)
  • 2021-01-03 21:01

    If it's a map then you can convert to JSON. First import convert package from flutter.

    import 'dart:convert';
    

    then convert to JSON and print

    print(json.encode(yourMapVariable));
    
    0 讨论(0)
  • 2021-01-03 21:01

    Try this one..

      void printWrapped(String text) {
        final pattern = new RegExp('.{1,800}'); // 800 is the size of each chunk
        pattern.allMatches(text).forEach((match) => print(match.group(0)));
      }
    
    0 讨论(0)
  • 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); 
    
    0 讨论(0)
提交回复
热议问题