saveStringLocally method
Saves the model's serialized JSON string to local storage. Ensures only one write happens at a time (simple async lock).
Notes:
- Blocks other writers via
_isWriting - Writes atomically (one file write operation)
- Overwrites a single file: local_data.txt
Implementation
Future<void> saveStringLocally(String value) async {
// Wait until other write operations finish
while (_isWriting) {
await Future.delayed(Duration(milliseconds: 1));
}
_isWriting = true;
try {
final file = File('local_data.txt');
// Write the JSON string to disk
await file.writeAsString(value);
} finally {
// Release the lock even if an exception occurs
_isWriting = false;
}
}