Problem & Constraints
Scaling recipes and merging ingredients into a single grocery list gets messy fast when recipes mix volume and weight units (such as cups, tablespoons, ounces, and grams) across ingredients with different densities. I built RecipeVault as an offline-first mobile app that delegates unit parsing, scaling, and density-aware conversion to a portable C++20 backend rather than duplicating fragile conversion logic in UI code.
Architecture & Tradeoffs
- C++20 Core over
dart:ffi: Instead of routing computation through asynchronous Flutter Platform Channels, RecipeVault calls directly into a compiled C++20 shared library over a flatextern "C"ABI. This keeps conversions synchronous, deterministic, and reusable outside Flutter. - Offline-First SQLite (
sqflite): All recipes, ingredient lists, and active shopping-list checkoffs persist locally in SQLite so kitchen prep and grocery store trips work reliably without network dependency. - Narrow FFI Boundary: Passing compact value structs and explicit error codes across the native boundary avoids excessive Dart-to-C++ heap allocations and keeps platform build integration clean through CMake.
Interop & Memory Ownership Pattern
Keeping the native interface strictly C-compatible and explicitly defining pointer ownership prevents subtle leaks across release builds:
// native/unit_converter.h
extern "C" {
struct ConversionResult {
double value;
int32_t status_code;
};
ConversionResult rv_convert_unit(
double amount,
int32_t from_unit_id,
int32_t to_unit_id,
double grams_per_ml);
}
// lib/native/unit_bridge.dart
final class ConversionResult extends Struct {
@Double()
external double value;
@Int32()
external int statusCode;
}
typedef _NativeConvert = ConversionResult Function(
Double amount,
Int32 fromUnitId,
Int32 toUnitId,
Double gramsPerMl,
);
typedef _DartConvert = ConversionResult Function(
double amount,
int fromUnitId,
int toUnitId,
double gramsPerMl,
);
Debugging & Packaging Win
- Cross-Platform Symbol Loading & Packaging: Standardizing
DynamicLibrary.openresolution on Android (.so) versus statically linked process symbols on Apple/desktop targets—and verifying native library packaging in release APK builds—ensured identical behavior between debug emulators and physical devices.