'How to save and create files in internal device storage flutter

How can I save and create files in internal device storage in flutter? When I create file with getExternalStorageDirectory(), the file hasn't appeared in internal storage.

          String fileExtension = name!.split('.').last;
          String fileName =
              name!.replaceAll('.${fileExtension}', '');

          String? localPath = await _findLocalPath();

          String completePath = '${localPath}/${fileName}.${fileExtension}';

          logger.e(completePath);
          io.File file = io.File(completePath);
          await file.writeAsBytes(dataStore);
          file.create();
Future<String?> _findLocalPath() async {
    String? externalStorageDirPath;
    if (io.Platform.isAndroid) {
      final io.Directory? directory = await getExternalStorageDirectory();
      externalStorageDirPath = directory!.path;
    } else if (io.Platform.isIOS) {
      externalStorageDirPath =
          (await getApplicationDocumentsDirectory()).absolute.path;
    }
    final io.Directory savedDir = io.Directory(externalStorageDirPath!);
    bool hasExisted = await savedDir.exists();
    logger.e(hasExisted);
    if (!hasExisted) {
      logger.e('Create Folder');
      savedDir.create();
    }
    return externalStorageDirPath;
  }

Can anyone help me?



Solution 1:[1]

Try replacing the top block of code with this:

      String fileExtension = name!.split('.').last;
      String fileName =
          name!.replaceAll('.${fileExtension}', '');

      String? localPath = await _findLocalPath();

      String completePath = '${localPath}/${fileName}.${fileExtension}';

      logger.e(completePath);
      io.File file = io.File(completePath);
      //await file.writeAsBytes(dataStore);
      //file.create();
      var output = file.openWrite(mode: FileMode.writeOnlyAppend);

      await for (final data in dataStore) {
        output.add(data);
      }

      await output.close();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Eduard