GitHub Repository:
Building a basic calculator that adds and subtracts is straightforward. Building one that handles trigonometric functions, logarithms, and high-precision computations — with a live API backend and a seamless offline fallback — is where real mobile engineering begins.
Kalkulator API RA bridges this gap — it is a Flutter-based scientific calculator that offloads complex mathematical operations to the MathJS API, while gracefully degrading to local Dart computation when the network is unavailable. The result is a calculator that is both powerful and resilient, regardless of connectivity.
This article covers the complete architecture: from API integration and fallback design to state management and error handling.
The Calculator Architecture Pipeline
+----------------+ +----------------+ +----------------+
| User Input | | API Layer | | Result |
| | | | | |
| Basic Mode |---->| MathJS API |---->| Display |
| Scientific Mode| | (Primary) | | Formatted |
| | | | | Output |
| sin, cos, tan | | dart:math | | |
| log, ln, sqrt | | (Fallback) | | Error Message |
+----------------+ +----------------+ +----------------+
Before writing any logic, set up the Flutter project with the required dependencies.
Add the following to your pubspec.yaml:
Then install:
lib/
├── main.dart # Entry point and main widget
├── calculator_state.dart # State management
├── api_service.dart # HTTP service layer
└── utils/
├── math_functions.dart # Local math calculations
└── formatters.dart # Number formatting utilities
Step Two: API Integration
The calculator uses the MathJS API as its primary computation engine, sending raw expressions and receiving evaluated results.
The app checks connectivity at startup and provides a manual test button in the app bar:
Step Three: Fallback System
When the API is unavailable, the app automatically switches to local computation using dart:math. The user experience remains uninterrupted.
Step Four: State Management
The calculator state handles mode switching, input accumulation, and result display.
Step Five: Error Handling
The app handles multiple failure scenarios to ensure the user always receives a meaningful response.
| Error Type | Cause | Handling Strategy |
| ---------------- | ------------------ | ------------------------ |
| SocketException | No internet | Switch to local fallback |
| TimeoutException | API too slow (15s) | Switch to local fallback |
| FormatException | Invalid expression | Show error message |
| HTTP Error | API unavailable | Switch to local fallback |
| Division by zero | User input | Show protected message |
Handle SSL in Development
During development, you may encounter SSL certificate issues with the MathJS API. Use this override only in development — never in production:
Cache Repeated Expressions
Avoid hitting the API for the same expression twice in the same session:
Prevent firing API requests on every keystroke by debouncing the calculation trigger:
The journey from a basic four-function calculator to a resilient, API-backed scientific calculator requires careful thinking at every layer — from network handling and fallback strategies to state management and error boundaries.
The key lesson from this project: offline-first is not optional. A calculator that stops working when the API is down is worse than no calculator at all. Design the fallback first, integrate the API second, and make sure the user never sees a broken state no matter what the network does.