Common Mistakes When Working with Files and JSON in Dart and C# and How to Avoid Them

Common Mistakes When Working with Files and JSON in Dart and C# and How to Avoid Them

Yehor Yurkevycn

Working with files and JSON is a key topic in Nexus Bundle and Arc Suite. Here are the most common beginner mistakes and ways to avoid them.

  1. Unhandled exceptions on file read/write — program crashes if file is missing or locked. Avoidance: always wrap in try/catch around File.ReadAllText / File.WriteAllText (C#) or File.readAsStringSync / writeAsStringSync (Dart). Show user message: "File not found, creating new".
  2. JSON format errors — jsonDecode fails on invalid JSON. Avoidance: wrap in try/catch (FormatException in Dart, JsonException in C#). Validate data before saving.
  3. Forgot to close file or stream — resource leaks in large programs. Avoidance: use using blocks in C#; Dart handles automatically or use try-finally.
  4. Invalid input before saving — empty or incorrect contacts saved. Avoidance: add validation (if (name.isEmpty) continue;). Covered in Input Safety in Arc Suite.
  5. Encoding issues — Cyrillic characters corrupted. Avoidance: specify UTF-8 explicitly on read/write (Encoding.UTF8 in C#, utf8 in Dart).
  6. Overwriting file without check — data loss on error. Avoidance: read first, modify in memory, then write.

These mistakes happen often in practice. Our materials explain them with examples, and tasks help practice fixes. If you spot one in your code — check Nexus Bundle modules; they provide all tools for reliable code.

Back to blog