Proper syntax for directly printing instance properties to console in Dart (not simply printing a variable that is in scope)

926

You need to enclose the property in curly braces:

print('the thing I want to see in the console is: ${a.property}');

That will then print the value of a.property.

Share:
926
Nerdy Bunz
Author by

Nerdy Bunz

Hi. Coding and creating new things gets me excited like a fizzy drink, but sometimes I run into headaches, so I come here for help. But I make sure to help other people too, so I think it works out. Apparently my posts often contain language considered extraneous by the pool cleaning robots who dutifully scrub it away on their regular patrols. "Bzzzz! Bzzzz!!! Desaturate, desaturate!" Fortunately, it's ACCESS DENIED for any editing attempts on this lil' article. They'll have to re-route the encryptions... and good luck with that. Oooo! Butterfly!!!! Anyway... when I'm not on here, I like to spend time sniffing and paddling my way around the enchanted river-forest near my house... and knitting my very own apps.

Updated on December 12, 2022

Comments

  • Nerdy Bunz
    Nerdy Bunz over 1 year

    In Dart/Flutter, suppose you have an instance a of Class Y.

    Class Y has a property, property1.

    You want to print that property using string interpolation like so:

    print('the thing I want to see in the console is: $a.property1');
    

    But you can't even finish typing that in without getting an error.

    The only way I can get it to work is by doing this:

    var temp = a.property1;
    print ('the thing I want to see in the console is: $temp');
    

    I haven't found the answer online... and me thinks there must be a way to just do it directly without having to create a variable first.