@@ -839,6 +839,24 @@ final Map<Type, NyApiService> apiDecoders = {${reg.allMatches(file).map((e) => e
839839 await _createNewFile (filePath, value);
840840 }
841841
842+ /// Create a new file at a [path] with a [value] .
843+ /// You can override an existing file by setting [overrideFile] to true.
844+ /// You can also set the [fileMode] , [encoding] and [flush] options
845+ static Future <bool > createFile (
846+ String path,
847+ value, {
848+ bool overrideFile = false ,
849+ FileMode fileMode = FileMode .write,
850+ Encoding encoding = utf8,
851+ bool flush = false ,
852+ }) async {
853+ return await _createNewFile (path, value,
854+ overrideFile: overrideFile,
855+ fileMode: fileMode,
856+ encoding: encoding,
857+ flush: flush);
858+ }
859+
842860 /// Check if a file exist by passing in a [path] .
843861 static Future <bool > hasFile (String path) async => await File (path).exists ();
844862
@@ -1129,14 +1147,31 @@ extension IterableExtension<T> on Iterable<T> {
11291147}
11301148
11311149/// Creates a new file from a [path] and [value] .
1132- Future <void > _createNewFile (String path, String value,
1133- {Function ()? onSuccess}) async {
1150+ Future <bool > _createNewFile (String path, String value,
1151+ {Function ()? onSuccess,
1152+ bool overrideFile = true ,
1153+ FileMode fileMode = FileMode .write,
1154+ Encoding encoding = utf8,
1155+ bool flush = false }) async {
11341156 final File file = File (path);
1135- File fileCreated = await file.writeAsString (value);
1157+ if ((await file.exists ())) {
1158+ if (! overrideFile) {
1159+ return false ;
1160+ }
1161+ FileSystemEntity fileSystemEntity = await file.delete ();
1162+ if (await fileSystemEntity.exists ()) {
1163+ return false ;
1164+ }
1165+ }
1166+
1167+ File fileCreated = await file.writeAsString (value,
1168+ mode: fileMode, encoding: encoding, flush: flush);
1169+
11361170 if (await fileCreated.exists ()) {
1137- if (onSuccess == null ) return ;
1171+ if (onSuccess == null ) return true ;
11381172 onSuccess ();
11391173 }
1174+ return true ;
11401175}
11411176
11421177/// Creates a new directory from a [path] if it doesn't exist.
0 commit comments