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),
),
);
}
}