-
Notifications
You must be signed in to change notification settings - Fork 3
/
path_provider_screen.dart
140 lines (128 loc) · 4.1 KB
/
path_provider_screen.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
class PathProviderScreen extends StatefulWidget {
@override
_PathProviderScreenState createState() => _PathProviderScreenState();
}
class _PathProviderScreenState extends State<PathProviderScreen> {
Future<Directory> _tempDirectory;
Future<Directory> _appSupportDirectory;
Future<Directory> _appDocumentsDirectory;
Future<Directory> _externalDocumentsDirectory;
String writeResult = 'Click to write';
String readResult = 'Click to read';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('PathProvider Demo'),
),
body: Center(
child: ListView(
children: <Widget>[
ListTile(
title: Text('Get Temporary Directory'),
onTap: _requestTempDirectory,
subtitle: FutureBuilder(
future: _tempDirectory, builder: _buildDirectory),
),
ListTile(
title: Text('Get Application Documents Directory'),
onTap: _requestAppDocumentsDirectory,
subtitle: FutureBuilder(
future: _appDocumentsDirectory, builder: _buildDirectory),
),
ListTile(
title: Text(
'${Platform.isAndroid ? "App support directories are unavailable on Android" : 'Get Application Support Directory'}'),
onTap: Platform.isAndroid ? null : _requestAppSupportDirectory,
subtitle: FutureBuilder(
future: _appSupportDirectory, builder: _buildDirectory),
),
ListTile(
title: Text(
'${Platform.isIOS ? "External directories are unavailable " "on iOS" : "Get External Storage Directory"}'),
onTap: Platform.isIOS ? null : _requestExternalStorageDirectory,
subtitle: FutureBuilder(
future: _externalDocumentsDirectory,
builder: _buildDirectory),
),
ListTile(
title: Text('Write data to the file'),
subtitle: Text(writeResult),
onTap: _writeData,
),
ListTile(
title: Text('Read data from the file'),
subtitle: Text(readResult),
onTap: _readData,
),
],
),
),
);
}
Widget _buildDirectory(
BuildContext context, AsyncSnapshot<Directory> snapshot) {
Text text = const Text('');
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
text = Text('Error: ${snapshot.error}');
} else if (snapshot.hasData) {
text = Text('path: ${snapshot.data.path}');
} else {
text = const Text('path unavailable');
}
}
return text;
}
void _requestTempDirectory() {
setState(() {
_tempDirectory = getTemporaryDirectory();
});
}
void _requestAppDocumentsDirectory() {
setState(() {
_appDocumentsDirectory = getApplicationDocumentsDirectory();
});
}
void _requestAppSupportDirectory() {
setState(() {
_appSupportDirectory = getApplicationSupportDirectory();
});
}
void _requestExternalStorageDirectory() {
setState(() {
_externalDocumentsDirectory = getExternalStorageDirectory();
});
}
Future<File> _loadFile() async {
if (_appDocumentsDirectory == null) {
_requestAppDocumentsDirectory();
}
final dir = await _appDocumentsDirectory;
final path = dir.path;
return File('$path/message.txt');
}
void _writeData() async {
File file = await _loadFile();
try {
await file.writeAsString('Hello!');
writeResult = 'Write succeeded';
} catch (e) {
writeResult = 'Write failed: $e';
}
setState(() {});
}
void _readData() async {
File file = await _loadFile();
try {
String content = await file.readAsString();
readResult = 'Read succeeded: "$content"';
} catch (e) {
readResult = 'Read failed: $e';
}
setState(() {});
}
}