Skip to content

Commit 079fafa

Browse files
authored
Merge pull request #147 from Voklen/master
Support non-ASCII characters
2 parents 52a8e3f + cda1510 commit 079fafa

3 files changed

Lines changed: 14 additions & 11 deletions

File tree

docs/Usage/Storage Access Framework.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ final Uint8List? fileContent = await getDocumentContent(uri);
161161
/// Handle [fileContent]...
162162
163163
/// If the file is intended to be human readable, you can convert the output to [String]:
164-
print(String.fromCharCodes(fileContent));
164+
print(utf8.decode(fileContent));
165165
```
166166

167167
### <samp>getRealPathFromUri</samp>
@@ -253,7 +253,7 @@ final DocumentFile? createdFile = createFileAsBytes(
253253
parentUri,
254254
mimeType: 'text/plain',
255255
displayName: 'Sample File Name',
256-
bytes: Uint8List.fromList(fileContent.codeUnits),
256+
bytes: Uint8List.fromList(utf8.encode(fileContent)),
257257
);
258258
```
259259

@@ -272,14 +272,14 @@ final String fileContent = 'My File Content';
272272
/// Write to a file using a [Uint8List] as file contents [bytes]
273273
final bool? success = writeToFileAsBytes(
274274
documentUri,
275-
bytes: Uint8List.fromList(fileContent.codeUnits),
275+
bytes: Uint8List.fromList(utf8.encode(fileContent)),
276276
mode: FileMode.write,
277277
);
278278
279279
/// Append to a file using a [Uint8List] as file contents [bytes]
280280
final bool? success = writeToFileAsBytes(
281281
documentUri,
282-
bytes: Uint8List.fromList(fileContent.codeUnits),
282+
bytes: Uint8List.fromList(utf8.encode(fileContent)),
283283
mode: FileMode.write,
284284
);
285285
```
@@ -602,7 +602,7 @@ final DocumentFile? createdFile = createFile(
602602
parentUri,
603603
mimeType: 'text/plain',
604604
displayName: 'Sample File Name',
605-
content: Uint8List.fromList(fileContent.codeUnits),
605+
content: Uint8List.fromList(utf8.encode(fileContent)),
606606
);
607607
```
608608

@@ -637,14 +637,14 @@ final bool? success = writeToFile(
637637
/// Write to a file using a [Uint8List] as file contents [bytes]
638638
final bool? success = writeToFile(
639639
documentUri,
640-
content: Uint8List.fromList(fileContent.codeUnits),
640+
content: Uint8List.fromList(utf8.encode(fileContent)),
641641
mode: FileMode.write,
642642
);
643643
644644
/// Append to a file using a [Uint8List] as file contents [bytes]
645645
final bool? success = writeToFile(
646646
documentUri,
647-
content: Uint8List.fromList(fileContent.codeUnits),
647+
content: Uint8List.fromList(utf8.encode(fileContent)),
648648
mode: FileMode.append,
649649
);
650650
```

example/lib/utils/document_file_utils.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:convert';
2+
13
import 'package:fl_toast/fl_toast.dart';
24
import 'package:flutter/material.dart';
35
import 'package:flutter/services.dart';
@@ -67,7 +69,7 @@ extension ShowDocumentFileContents on DocumentFile {
6769
return Image.memory(content);
6870
}
6971

70-
final contentAsString = String.fromCharCodes(content);
72+
final contentAsString = utf8.decode(content);
7173

7274
final fileIsEmpty = contentAsString.isEmpty;
7375

lib/src/saf/saf.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:async';
2+
import 'dart:convert';
23
import 'dart:io';
34
import 'dart:typed_data';
45

@@ -312,7 +313,7 @@ Future<DocumentFile?> createFileAsString(
312313
parentUri,
313314
displayName: displayName,
314315
mimeType: mimeType,
315-
bytes: Uint8List.fromList(content.codeUnits),
316+
bytes: Uint8List.fromList(utf8.encode(content)),
316317
);
317318
}
318319

@@ -384,7 +385,7 @@ Future<bool?> writeToFileAsString(
384385
}) {
385386
return writeToFileAsBytes(
386387
uri,
387-
bytes: Uint8List.fromList(content.codeUnits),
388+
bytes: Uint8List.fromList(utf8.encode(content)),
388389
mode: mode,
389390
);
390391
}
@@ -573,7 +574,7 @@ Future<String?> getDocumentContentAsString(
573574
}) async {
574575
final Uint8List? bytes = await getDocumentContent(uri);
575576

576-
return bytes?.apply((Uint8List a) => String.fromCharCodes(a));
577+
return bytes?.apply((Uint8List a) => utf8.decode(a));
577578
}
578579

579580
/// {@template sharedstorage.saf.getDocumentContentAsString}

0 commit comments

Comments
 (0)