BNetz
BNetz

Reputation: 361

Dart/Flutter: How to print something into the console from constructor

I want to print something to the console from my constructor as shown in the snippet below, but I get the error

Context: 'SeitenzahlenrechnerErgebnis.print' is defined here.
  print(a);

How/where can I execute the print(a)?

class MyClass extends StatefulWidget {
  MyClass({this.test});
  int a = 77;
  // I would like to say
  // print(a);
  // here, but no way …

  @override
  _MyClassState createState() =>
      _MyClassState();
}
…

I am new to Flutter/Dart - would appreciate help a lot. Thanks!

Upvotes: 3

Views: 1072

Answers (1)

pedro pimont
pedro pimont

Reputation: 3084

You have to open a block body after the constructor and print it there.

void main() {
  Dog myDog = Dog();
}

class Dog {
  Dog() {
    print('woof');
  } 
}

Your case:

class MyClass extends StatefulWidget {
  MyClass({this.test}) {
    print(a);
  }
  int a = 77;

  @override
  _MyClassState createState() =>
      _MyClassState();
}
…

Upvotes: 3

Related Questions