In flutter mobile app development language there are 2 inbuilt application debugging methods available print() and debugPrint(). They both are used to print debug messages on Terminal command prompt window. The print() method can only print a Object String to Terminal window whenever developer wants to. The debugPrint() method can print large size strings outputs on debugging terminal window. debugPrint() method can also print Flutter tools log on Terminal screen. So in this tutorial we would Print Console Log Message in Flutter App for Testing Purpose in Android iOS Example Tutorial.
Contents in this project Print Console Log Message in Flutter App for Testing Purpose in Android iOS Example Tutorial:
1. Import material.dart package in your project’s main.dart file.
1 |
import 'package:flutter/material.dart'; |
2. Create void main runApp() method and here we would call our main MyApp() class.
1 |
void main() => runApp(MyApp()); |
3. Create our main Root View class named as MyApp extends StatelessWidget.
1 2 3 4 |
class MyApp extends StatelessWidget { } |
4. Create a function named as showConsoleUsingPrint(). Inside the function we would call the print() method and prints a sample log message on terminal screen.
1 2 3 4 5 |
void showConsoleUsingPrint() { print('Console Message Using Print'); } |
5. Create a function named as showConsoleUsingDebugPrint(). Inside this function we would call the debugPrint() method and prints a text string message on terminal window.
1 2 3 4 5 |
void showConsoleUsingDebugPrint() { debugPrint('Console Message Using Debug Print'); } |
6. Create 2 Raised Buttons in Widget build area of MyApp class. We would call each above function on Button onPress event.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
Widget build(BuildContext context) { print('Widget Build Start'); return MaterialApp( home: Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Container( margin: const EdgeInsets.fromLTRB(10, 10, 10, 10), child: RaisedButton( onPressed: () => showConsoleUsingPrint(), child: Text(' Print Console Message using Print '), textColor: Colors.white, color: Colors.green, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), Container( margin: const EdgeInsets.fromLTRB(10, 10, 10, 10), child: RaisedButton( onPressed: () => showConsoleUsingDebugPrint(), child: Text(' Print Console Message using Debug Print '), textColor: Colors.white, color: Colors.green, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), ]) ) ) ); } |
7. Complete source code for main.dart file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { void showConsoleUsingPrint() { print('Console Message Using Print'); } void showConsoleUsingDebugPrint() { debugPrint('Console Message Using Debug Print'); } @override Widget build(BuildContext context) { print('Widget Build Start'); return MaterialApp( home: Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Container( margin: const EdgeInsets.fromLTRB(10, 10, 10, 10), child: RaisedButton( onPressed: () => showConsoleUsingPrint(), child: Text(' Print Console Message using Print '), textColor: Colors.white, color: Colors.green, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), Container( margin: const EdgeInsets.fromLTRB(10, 10, 10, 10), child: RaisedButton( onPressed: () => showConsoleUsingDebugPrint(), child: Text(' Print Console Message using Debug Print '), textColor: Colors.white, color: Colors.green, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), ]) ) ) ); } } |
Screenshots:
