عمليات نظام الملفات
تعلّم القراءة من نظام ملفات الجهاز والكتابة إليه، مما يتيح تخزين الملفات الأكبر حجمًا وتنسيقات البيانات المخصّصة
عمليات نظام الملفات درس مجاني في Flutter Mobile Development على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flutter Mobile Development، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flutter Mobile Development 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Storing Data in Files
Sometimes, your app needs to store data directly on the device. This is where file system operations come in! Unlike shared_preferences (for small key-value data) or sqflite (for structured data), file system operations allow you to manage files directly.
You can store larger pieces of data, like images, audio, or custom formatted text files, that don't fit well into a database or simple key-value pairs.
Where to Store Files?
Before you can read or write files, your app needs to know where it can store them. Flutter provides the path_provider package to help you find common directory paths safely across different platforms (iOS, Android, web, desktop).
- Application Documents Directory: This is where your app can store user-specific data that should persist.
- Temporary Directory: For data that doesn't need to persist across app launches, like cached files.
Getting Application Path
Let's use path_provider to get the application documents directory. Remember to add path_provider to your pubspec.yaml first!
dependencies:
flutter:
sdk: flutter
path_provider: ^2.0.11
import 'dart:io';
import 'package:path_provider/path_provider.dart';
void main() async {
// Get the application documents directory
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
print('App Documents Path: $appDocPath');
}Creating a File Reference
Once you have a directory path, you can create a File object. This object represents the file at that specific path on the device's storage. It doesn't mean the file actually exists yet, it's just a reference.
You'll combine the directory path with your desired filename to construct the full file path.
Constructing a File Path
Here's how you can combine the application documents path with a filename to create a File object. We'll use a simple text file name for our example.
import 'dart:io';
import 'package:path_provider/path_provider.dart';
void main() async {
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
// Define a filename
String fileName = 'my_data.txt';
// Create a File object
File file = File('$appDocPath/$fileName');
print('File path reference created: ${file.path}');
// At this point, the file might not exist on disk yet.
}Saving Content to Files
The File object provides methods to write data. The most common ones are writeAsString() for text and writeAsBytes() for binary data. These operations are asynchronous, so you'll use await.
writeAsString(String contents, {FileMode mode = FileMode.write}): Writes text.FileMode.writeoverwrites,FileMode.appendadds to the end.writeAsBytes(List<int> bytes, {FileMode mode = FileMode.write}): Writes raw bytes.
Writing a Simple Text File
Let's write some text content to our my_data.txt file. If the file doesn't exist, writeAsString will create it. If it does exist, it will overwrite its content by default.
import 'dart:io';
import 'package:path_provider/path_provider.dart';
void main() async {
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
String fileName = 'my_data.txt';
File file = File('$appDocPath/$fileName');
String content = 'Hello, CoddyKit learners!\nThis is a test message.';
try {
await file.writeAsString(content);
print('Content written to ${file.path}');
print('File exists: ${await file.exists()}');
} catch (e) {
print('Error writing file: $e');
}
}Reading Content from Files
Just as you can write, you can also read content back from files. The readAsString() method is perfect for text files, while readAsBytes() is used for binary data.
It's good practice to check if a file exists() before trying to read from it to prevent errors.
Reading a File's Content
Now, let's read the content we just wrote back from my_data.txt. We'll first check if the file exists to be safe.
import 'dart:io';
import 'package:path_provider/path_provider.dart';
void main() async {
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
String fileName = 'my_data.txt';
File file = File('$appDocPath/$fileName');
if (await file.exists()) {
try {
String content = await file.readAsString();
print('Content read from ${file.path}:\n$content');
} catch (e) {
print('Error reading file: $e');
}
} else {
print('File does not exist: ${file.path}');
}
}Removing Files
When data is no longer needed, you can delete files from the device's storage. The delete() method on a File object will remove it. This operation is also asynchronous.
Be careful when deleting files, as this action is usually irreversible!
Check Your Understanding
Which of the following statements about Flutter file system operations are TRUE?
Lesson Summary
Great job! In this lesson, you've learned how to interact with the device's file system in Flutter:
- Used
path_providerto find appropriate storage directories. - Created
Fileobjects to reference specific files. - Wrote content to files using
writeAsString(). - Read content from files using
readAsString(). - Understood how to delete files with
delete().
File system operations are crucial for storing large, custom-formatted, or sensitive data directly on the device. Next, you can explore more advanced data handling or move on to other Flutter topics!
الأسئلة الشائعة
هل درس «عمليات نظام الملفات» مجاني؟
نعم — نص درس «عمليات نظام الملفات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flutter Mobile Development، انتقل إلى CoddyKit PRO. تتضمن دورة Flutter Mobile Development 4 دروس في المجموع.
ماذا ستتعلم في «عمليات نظام الملفات»؟
تعلّم القراءة من نظام ملفات الجهاز والكتابة إليه، مما يتيح تخزين الملفات الأكبر حجمًا وتنسيقات البيانات المخصّصة تتمرن على Flutter Mobile Development مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Flutter Mobile Development؟
لا تُشترط خبرة سابقة. Flutter Mobile Development على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «عمليات نظام الملفات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Flutter Mobile Development هذا؟
نعم. كل درس في Flutter Mobile Development يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- Shared Preferences للبيانات
- دمج قاعدة بيانات SQLite
- عمليات نظام الملفات
- Hive والتخزين المحلي NoSQL