•  


Read and write files | Flutter

Read and write files

In some cases, you need to read and write files to disk. For example, you might need to persist data across app launches, or download data from the internet and save it for later offline use.

To save files to disk on mobile or desktop apps, combine the path_provider plugin with the dart:io library.

This recipe uses the following steps:

  1. Find the correct local path.
  2. Create a reference to the file location.
  3. Write data to the file.
  4. Read data from the file.

To learn more, watch this Package of the Week video on the path_provider package:

1. Find the correct local path

#

This example displays a counter. When the counter changes, write data on disk so you can read it again when the app loads. Where should you store this data?

The path_provider package provides a platform-agnostic way to access commonly used locations on the device's file system. The plugin currently supports access to two file system locations:

Temporary directory
A temporary directory (cache) that the system can clear at any time. On iOS, this corresponds to the NSCachesDirectory . On Android, this is the value that getCacheDir() returns.
Documents directory
A directory for the app to store files that only it can access. The system clears the directory only when the app is deleted. On iOS, this corresponds to the NSDocumentDirectory . On Android, this is the AppData directory.

This example stores information in the documents directory. You can find the path to the documents directory as follows:

dart
import
 'package:path_provider/path_provider.dart'
;

// ···

  Future
<
String
> 
get
 _localPath 
async
 {

    final
 directory = 
await
 getApplicationDocumentsDirectory
();


    return
 directory.path;

  }

2. Create a reference to the file location

#

Once you know where to store the file, create a reference to the file's full location. You can use the File class from the dart:io library to achieve this.

dart
Future
<
File
> 
get
 _localFile 
async
 {

  final
 path = 
await
 _localPath;

  return
 File
(
'
$
path
/counter.txt'
);

}

3. Write data to the file

#

Now that you have a File to work with, use it to read and write data. First, write some data to the file. The counter is an integer, but is written to the file as a string using the '$counter' syntax.

dart
Future
<
File
> 
writeCounter
(
int
 counter) 
async
 {

  final
 file = 
await
 _localFile;


  // Write the file

  return
 file.
writeAsString
(
'
$
counter
'
);

}

4. Read data from the file

#

Now that you have some data on disk, you can read it. Once again, use the File class.

dart
Future
<
int
> 
readCounter
() 
async
 {

  try
 {

    final
 file = 
await
 _localFile;


    // Read the file

    final
 contents = 
await
 file.
readAsString
();


    return
 int
.
parse
(contents);

  } 
catch
 (e) {

    // If encountering an error, return 0

    return
 0
;

  }

}

Complete example

#
dart
import
 'dart:async'
;

import
 'dart:io'
;


import
 'package:flutter/material.dart'
;

import
 'package:path_provider/path_provider.dart'
;


void
 main
() {

  runApp
(

    MaterialApp
(

      title: 
'Reading and Writing Files'
,

      home: 
FlutterDemo
(storage: 
CounterStorage
()),

    ),

  );

}


class
 CounterStorage
 {

  Future
<
String
> 
get
 _localPath 
async
 {

    final
 directory = 
await
 getApplicationDocumentsDirectory
();


    return
 directory.path;

  }


  Future
<
File
> 
get
 _localFile 
async
 {

    final
 path = 
await
 _localPath;

    return
 File
(
'
$
path
/counter.txt'
);

  }


  Future
<
int
> 
readCounter
() 
async
 {

    try
 {

      final
 file = 
await
 _localFile;


      // Read the file

      final
 contents = 
await
 file.
readAsString
();


      return
 int
.
parse
(contents);

    } 
catch
 (e) {

      // If encountering an error, return 0

      return
 0
;

    }

  }


  Future
<
File
> 
writeCounter
(
int
 counter) 
async
 {

    final
 file = 
await
 _localFile;


    // Write the file

    return
 file.
writeAsString
(
'
$
counter
'
);

  }

}


class
 FlutterDemo
 extends
 StatefulWidget
 {

  const
 FlutterDemo
({
super
.key, 
required
 this
.storage});


  final
 CounterStorage
 storage;


  @override

  State
<
FlutterDemo
> 
createState
() => 
_FlutterDemoState
();

}


class
 _FlutterDemoState
 extends
 State
<
FlutterDemo
> {

  int
 _counter = 
0
;


  @override

  void
 initState
() {

    super
.
initState
();

    widget.storage.
readCounter
().
then
((value) {

      setState
(() {

        _counter = value;

      });

    });

  }


  Future
<
File
> 
_incrementCounter
() {

    setState
(() {

      _counter++;

    });


    // Write the variable as a string to the file.

    return
 widget.storage.
writeCounter
(_counter);

  }


  @override

  Widget
 build
(
BuildContext
 context) {

    return
 Scaffold
(

      appBar: 
AppBar
(

        title: 
const
 Text
(
'Reading and Writing Files'
),

      ),

      body: 
Center
(

        child: 
Text
(

          'Button tapped 
$
_counter
 time
${
_counter
 == 1 ? 
''
 : 
's'
}
.'
,

        ),

      ),

      floatingActionButton: 
FloatingActionButton
(

        onPressed: _incrementCounter,

        tooltip: 
'Increment'
,

        child: 
const
 Icon
(
Icons
.add),

      ),

    );

  }

}
- "漢字路" 한글한자자동변환 서비스는 교육부 고전문헌국역지원사업의 지원으로 구축되었습니다.
- "漢字路" 한글한자자동변환 서비스는 전통문화연구회 "울산대학교한국어처리연구실 옥철영(IT융합전공)교수팀"에서 개발한 한글한자자동변환기를 바탕하여 지속적으로 공동 연구 개발하고 있는 서비스입니다.
- 현재 고유명사(인명, 지명등)을 비롯한 여러 변환오류가 있으며 이를 해결하고자 많은 연구 개발을 진행하고자 하고 있습니다. 이를 인지하시고 다른 곳에서 인용시 한자 변환 결과를 한번 더 검토하시고 사용해 주시기 바랍니다.
- 변환오류 및 건의,문의사항은 juntong@juntong.or.kr로 메일로 보내주시면 감사하겠습니다. .
Copyright ⓒ 2020 By '전통문화연구회(傳統文化硏究會)' All Rights reserved.
 한국   대만   중국   일본