Chris McDonald
Chris McDonald

Reputation: 163

Print function not working in Flutter Stateless Widget

I just want to be able to print a value in my Stateless Widget, but I'm running into some errors. Here's a simplified version of my code.

void main() {
  runApp(MaterialApp(
    home: Home(),
  ));
}

class Home extends StatelessWidget {
  print('test');
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // ... some other widgets here
    );
  }
}

I unfortunately keep getting these two errors.

Error: Expected an identifier, but got ''test''.
Try inserting an identifier before ''test''.
  print('test');
        ^^^^^^
Error: The non-abstract class 'Home' is missing implementations for these members:
 - Home.print

I'm very new to flutter, so any help will be greatly appreciated. Thanks!

Upvotes: 1

Views: 1924

Answers (1)

Nishuthan S
Nishuthan S

Reputation: 1735

Your print statement is outside the build function place it inside the function.

Like this:

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    print('test'); //This will work
    return Scaffold(
        // ... some other widgets here
        );
  }
}

Upvotes: 1

Related Questions