Explore the creation of a simple console-based calculator using Dart, focusing on arithmetic operations, user input handling, and error management.
In this section, we embark on an exciting journey to build a simple yet functional console-based calculator using Dart. This project serves as a practical application of the foundational concepts covered in the previous sections, such as variables, data types, control flow statements, and functions. By the end of this project, you’ll have a deeper understanding of how to structure a Dart program, handle user input and output, and manage basic error scenarios.
The Simple Calculator Console App is designed to perform basic arithmetic operations: addition, subtraction, multiplication, and division. This project is an excellent opportunity to apply your knowledge of Dart programming in a real-world scenario. The calculator will prompt users to enter two numbers and select an operation. It will then display the result of the computation, ensuring that invalid inputs and division by zero are handled gracefully.
The primary objectives of this project are:
The Simple Calculator Console App will include the following features:
To provide a clear understanding of the project’s workflow, let’s visualize the process using a Mermaid.js diagram:
flowchart TD A[Simple Calculator Console App] --> B[User Input] A --> C[Select Operation] A --> D[Perform Calculation] A --> E[Display Result] A --> F[Error Handling] B --> B1[Enter First Number] B --> B2[Enter Second Number] C --> C1[Choose +, -, *, /] D --> D1[Invoke Functions] F --> F1[Invalid Number] F --> F2[Division by Zero]
The first step in our calculator app is to capture user input. This involves prompting the user to enter two numbers. In Dart, we can use the stdin.readLineSync()
method to read input from the console. Here’s a simple example:
import 'dart:io';
void main() {
print('Enter the first number:');
String? input1 = stdin.readLineSync();
double number1 = double.parse(input1!);
print('Enter the second number:');
String? input2 = stdin.readLineSync();
double number2 = double.parse(input2!);
}
Key Points:
stdin.readLineSync()
method reads a line of text from the console.double.parse()
to convert the string input into a double for arithmetic operations.Once we have the two numbers, the next step is to allow the user to select an arithmetic operation. We can achieve this using a simple menu system:
print('Select an operation:');
print('1. Addition (+)');
print('2. Subtraction (-)');
print('3. Multiplication (*)');
print('4. Division (/)');
String? operation = stdin.readLineSync();
Key Points:
With the numbers and operation selected, we can perform the calculation. This involves using control flow statements to determine which operation to execute:
double result;
switch (operation) {
case '1':
result = number1 + number2;
break;
case '2':
result = number1 - number2;
break;
case '3':
result = number1 * number2;
break;
case '4':
if (number2 != 0) {
result = number1 / number2;
} else {
print('Error: Division by zero is not allowed.');
return;
}
break;
default:
print('Invalid operation selected.');
return;
}
print('The result is: $result');
Key Points:
switch
statement to determine the operation based on the user’s choice.Error handling is a crucial part of any application. In our calculator app, we need to manage two primary error scenarios: invalid number inputs and division by zero.
Invalid Number Input: We can use a try-catch
block to handle exceptions that occur when parsing the input:
try {
double number1 = double.parse(input1!);
} catch (e) {
print('Invalid input. Please enter a valid number.');
return;
}
Division by Zero: As shown in the calculation section, we check if the divisor is zero before performing the division.
Here’s the complete code for the Simple Calculator Console App:
import 'dart:io';
void main() {
print('Enter the first number:');
String? input1 = stdin.readLineSync();
double number1;
try {
number1 = double.parse(input1!);
} catch (e) {
print('Invalid input. Please enter a valid number.');
return;
}
print('Enter the second number:');
String? input2 = stdin.readLineSync();
double number2;
try {
number2 = double.parse(input2!);
} catch (e) {
print('Invalid input. Please enter a valid number.');
return;
}
print('Select an operation:');
print('1. Addition (+)');
print('2. Subtraction (-)');
print('3. Multiplication (*)');
print('4. Division (/)');
String? operation = stdin.readLineSync();
double result;
switch (operation) {
case '1':
result = number1 + number2;
break;
case '2':
result = number1 - number2;
break;
case '3':
result = number1 * number2;
break;
case '4':
if (number2 != 0) {
result = number1 / number2;
} else {
print('Error: Division by zero is not allowed.');
return;
}
break;
default:
print('Invalid operation selected.');
return;
}
print('The result is: $result');
}
To deepen your understanding of Dart and console applications, consider exploring the following resources:
The Simple Calculator Console App is a foundational project that consolidates your understanding of Dart basics. By building this application, you gain practical experience in program structuring, user interaction, and error management. As you continue your journey in Flutter development, these skills will serve as a solid foundation for more complex projects.