Print to the Console
Part of the Making It Interactive section of Coddy's Flutter journey. Lesson 2 of 34.
A tap on a button runs code you cannot see. print makes it visible: it writes a line of text to the Console, the panel under the phone.
ElevatedButton(
onPressed: () {
print('Button pressed');
},
child: Text('Press me'),
)Each line of code inside the braces ends with a semicolon ;. Tap the button three times and the Console shows Button pressed three times.
The person using your app never sees the Console. It is for you: when something does not work, a print tells you whether your code ran at all.
Challenge
EasyMake the Say hi button print Hi there! each time it is pressed. Press it a few times and watch the Console.
Try it yourself
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () {},
child: Text('Say hi'),
),
),
),
);
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Making It Interactive
Practice on your own: Online Flutter compiler