Kasikornbank Amorn Apichattanakul Apr 24, 2026

Predictive Mobile Agent #3: Installing the Brain — Bringing Your On-Device AI to Life in Flutter

Article Summary

Amorn Apichattanakul from KBTG reveals the hidden trap that breaks on-device AI: your Dart preprocessing code must match your Python training code bit-for-bit, or your 15KB model becomes useless.

This is part 3 of a series on building predictive mobile agents. After training a multi-head neural network in Python to predict payment behavior (should suggest? which shop? how much?), the author tackles the critical challenge of deploying it in Flutter without breaking the model's assumptions.

Key Takeaways

Critical Insight

The article demonstrates that 90% of on-device AI effort should go into preprocessing contract correctness, not model architecture, with detailed Dart ports of Python encoding functions.

The author shares what breaks when the model sees Saturday 1 AM walking sessions it never trained on, and hints at feeding these predictions to an LLM for dynamic UI generation in part 4.

About This Article

Problem

Amorn Apichattanakul found that Python's datetime.weekday() returns 0-6 for Monday through Sunday, while Dart's weekday returns 1-7. When the model received index 0 instead of 6 for Sunday, predictions failed silently. Friday family dinner predictions got worse, but no error messages appeared.

Solution

The team ported Python's encoding functions to Dart line by line. They applied the correction (dt.weekday - 1) % 7 to fix the weekday index mismatch. They also implemented trigonometric time encoding using dart:math for sin and cos operations to match Python's output exactly.

Impact

The unified preprocessing contract let the 15.70 KB model run inference in 1-2 ms on-device. Prediction accuracy stayed consistent across mock and live sensor modes. Six test scenarios verified the fix, including Rule 1 for morning coffee and Rule 7 for WFH guard patterns.