-
-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add writeToFile, writeToFileAsBytes, writeToFileAsString #85
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -217,6 +217,33 @@ final DocumentFile? createdFile = createFileAsBytes( | |||||
); | ||||||
``` | ||||||
|
||||||
### <samp>writeToFileAsBytes</samp> | ||||||
|
||||||
Write to a file using raw bytes `Uint8List`. | ||||||
|
||||||
Given the document uri, opens the file in the specified `mode` and writes the `bytes` to it. | ||||||
|
||||||
`mode` represents the mode in which the file will be opened for writing. Use `FileMode.write` for truncating and `FileMode.append` for appending to the file. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
```dart | ||||||
final Uri documentUri = ... | ||||||
final String fileContent = 'My File Content'; | ||||||
|
||||||
/// Write to a file using a [Uint8List] as file contents [bytes] | ||||||
final bool? success = writeToFileAsBytes( | ||||||
documentUri, | ||||||
bytes: Uint8List.fromList(fileContent.codeUnits), | ||||||
mode: FileMode.write, | ||||||
); | ||||||
|
||||||
/// Append to a file using a [Uint8List] as file contents [bytes] | ||||||
final bool? success = writeToFileAsBytes( | ||||||
documentUri, | ||||||
bytes: Uint8List.fromList(fileContent.codeUnits), | ||||||
mode: FileMode.write, | ||||||
); | ||||||
``` | ||||||
|
||||||
### <samp>canRead</samp> | ||||||
|
||||||
<samp>Mirror of [`DocumentFile.canRead`](<https://developer.android.com/reference/androidx/documentfile/provider/DocumentFile#canRead()>)</samp> | ||||||
|
@@ -485,6 +512,31 @@ final DocumentFile? createdFile = createFileAsString( | |||||
); | ||||||
``` | ||||||
|
||||||
### <samp>writeToFileAsString</samp> | ||||||
|
||||||
<samp>Alias for `writeToFileAsBytes`</samp> | ||||||
|
||||||
Convenient method to write to a file using `content` as `String` instead `Uint8List`. | ||||||
|
||||||
```dart | ||||||
final Uri documentUri = ... | ||||||
final String fileContent = 'My File Content'; | ||||||
|
||||||
/// Write to a file using a [Uint8List] as file contents [bytes] | ||||||
final bool? success = writeToFileAsString( | ||||||
documentUri, | ||||||
content: fileContent, | ||||||
mode: FileMode.write, | ||||||
); | ||||||
|
||||||
/// Append to a file using a [Uint8List] as file contents [bytes] | ||||||
final bool? success = writeToFileAsBytes( | ||||||
documentUri, | ||||||
content: fileContent, | ||||||
mode: FileMode.write, | ||||||
); | ||||||
``` | ||||||
|
||||||
### <samp>createFile</samp> | ||||||
|
||||||
<samp>Alias for `createFileAsBytes` and `createFileAsString`</samp> | ||||||
|
@@ -514,6 +566,50 @@ final DocumentFile? createdFile = createFile( | |||||
); | ||||||
``` | ||||||
|
||||||
### <samp>writeToFile</samp> | ||||||
|
||||||
<samp>Alias for `writeToFileAsBytes` and `writeToFileAsString`</samp> | ||||||
|
||||||
Convenient method to write to a file using `content` as `String` **or** `bytes` as `Uint8List`. | ||||||
|
||||||
You should provide either `content` or `bytes`, if both `bytes` will be used. | ||||||
|
||||||
`mode` represents the mode in which the file will be opened for writing. Use `FileMode.write` for truncating and `FileMode.append` for appending to the file. | ||||||
|
||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
```dart | ||||||
final Uri documentUri = ... | ||||||
final String fileContent = 'My File Content'; | ||||||
|
||||||
/// Write to a file using a [String] as file contents [content] | ||||||
final bool? success = writeToFile( | ||||||
documentUri, | ||||||
content: fileContent, | ||||||
mode: FileMode.write, | ||||||
); | ||||||
|
||||||
/// Append to a file using a [String] as file contents [content] | ||||||
final bool? success = writeToFile( | ||||||
documentUri, | ||||||
content: fileContent, | ||||||
mode: FileMode.append, | ||||||
); | ||||||
|
||||||
/// Write to a file using a [Uint8List] as file contents [bytes] | ||||||
final DocumentFile? createdFile = writeToFile( | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Guess this function returns a |
||||||
documentUri, | ||||||
content: Uint8List.fromList(fileContent.codeUnits), | ||||||
mode: FileMode.write, | ||||||
); | ||||||
|
||||||
/// Append to a file using a [Uint8List] as file contents [bytes] | ||||||
final DocumentFile? createdFile = writeToFile( | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Guess this function returns a |
||||||
documentUri, | ||||||
content: Uint8List.fromList(fileContent.codeUnits), | ||||||
mode: FileMode.append, | ||||||
); | ||||||
``` | ||||||
|
||||||
## External APIs (deprecated) | ||||||
|
||||||
These APIs are from external Android libraries. | ||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,4 +1,5 @@ | ||||||||||||||||
import 'dart:async'; | ||||||||||||||||
import 'dart:io'; | ||||||||||||||||
import 'dart:typed_data'; | ||||||||||||||||
|
||||||||||||||||
import '../../saf.dart'; | ||||||||||||||||
|
@@ -281,6 +282,82 @@ Future<DocumentFile?> createFileAsString( | |||||||||||||||
); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
/// {@template sharedstorage.saf.writeToFile} | ||||||||||||||||
/// Convenient method to write to a file using either String or raw bytes. | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
You can also apply this change for |
||||||||||||||||
/// | ||||||||||||||||
/// Under the hood this method calls `writeToFileAsString` or `writeToFileAsBytes` | ||||||||||||||||
/// depending on which argument is passed. | ||||||||||||||||
/// | ||||||||||||||||
/// If both (bytes and content) are passed, the bytes will be used and the content will be ignored. | ||||||||||||||||
/// {@endtemplate} | ||||||||||||||||
Future<bool?> writeToFile( | ||||||||||||||||
Uri uri, { | ||||||||||||||||
Uint8List? bytes, | ||||||||||||||||
String? content, | ||||||||||||||||
FileMode? mode, | ||||||||||||||||
}) { | ||||||||||||||||
assert( | ||||||||||||||||
bytes != null || content != null, | ||||||||||||||||
'''Either [bytes] or [content] should be provided''', | ||||||||||||||||
); | ||||||||||||||||
|
||||||||||||||||
return bytes != null | ||||||||||||||||
? writeToFileAsBytes( | ||||||||||||||||
uri, | ||||||||||||||||
bytes: bytes, | ||||||||||||||||
mode: mode, | ||||||||||||||||
) | ||||||||||||||||
: writeToFileAsString( | ||||||||||||||||
uri, | ||||||||||||||||
content: content!, | ||||||||||||||||
mode: mode, | ||||||||||||||||
); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
/// {@template sharedstorage.saf.writeToFileAsBytes} | ||||||||||||||||
/// Write to a file. | ||||||||||||||||
/// - `uri` is the URI of the file. | ||||||||||||||||
/// - `bytes` is the content of the document as a list of bytes `Uint8List`. | ||||||||||||||||
/// - `mode` is the mode in which the file will be opened for writing. Use `FileMode.write` for truncating and `FileMode.append` for appending to the file. | ||||||||||||||||
/// | ||||||||||||||||
/// Returns `true` if the file was successfully written to. | ||||||||||||||||
/// {@endtemplate} | ||||||||||||||||
Future<bool?> writeToFileAsBytes( | ||||||||||||||||
Uri uri, { | ||||||||||||||||
required Uint8List bytes, | ||||||||||||||||
FileMode? mode, | ||||||||||||||||
}) async { | ||||||||||||||||
var writeMode = 'wt'; | ||||||||||||||||
|
||||||||||||||||
if (mode == FileMode.append || mode == FileMode.writeOnlyAppend) { | ||||||||||||||||
writeMode = 'wa'; | ||||||||||||||||
} | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
|
||||||||||||||||
final args = <String, dynamic>{ | ||||||||||||||||
'uri': uri.toString(), | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
'content': bytes, | ||||||||||||||||
'mode': writeMode, | ||||||||||||||||
}; | ||||||||||||||||
|
||||||||||||||||
return kDocumentFileChannel.invokeMethod<bool>('writeToFile', args); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
/// {@template sharedstorage.saf.writeToFileAsString} | ||||||||||||||||
/// Convenient method to write to a file. | ||||||||||||||||
/// using `content` as String instead Uint8List. | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
You can also apply this change for |
||||||||||||||||
/// {@endtemplate} | ||||||||||||||||
Future<bool?> writeToFileAsString( | ||||||||||||||||
Uri uri, { | ||||||||||||||||
required String content, | ||||||||||||||||
FileMode? mode, | ||||||||||||||||
}) { | ||||||||||||||||
return writeToFileAsBytes( | ||||||||||||||||
uri, | ||||||||||||||||
bytes: Uint8List.fromList(content.codeUnits), | ||||||||||||||||
mode: mode, | ||||||||||||||||
); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
/// {@template sharedstorage.saf.length} | ||||||||||||||||
/// Equivalent to `DocumentFile.length`. | ||||||||||||||||
/// | ||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Syntax error because of this comma: