0

Из массива строк нужно создать текстовый файл. Пробую через цикл for дописывать строки в конце файла, но получается "обрезка" строк рисунок

Вот код метода для записи строк из массива:

    new RaisedButton(
      onPressed: () {
        //TODO записать в файл
        for(var i = 0; i < character.length; i++){
          String content = character[i];
          CharacterStorage().writeCaracter(content);
        }
      },
      child: new Text('Сохранить в файл',
      style: new TextStyle(fontSize: 16.0),),
    )

А вот код класса для записи файлов:

class CharacterStorage {
  String filename = 'characterist.txt';

  Future<String> get _localPath async {
    final directory = await getApplicationDocumentsDirectory();

    return directory.path;
  }

  Future<File> get _localFile async {
    final path = await _localPath;
    return new File('$path/$filename');
  }

  Future<String> readCounter() async {
    try {
      final file = await _localFile;

      // Read the file
      String contents = await file.readAsString();

      return contents;
    } catch (e) {
      return 'READING FILE ERROR';
    }
  }

  Future<File> writeCaracter(String content) async {
    final file = await _localFile;
    // Write the file
    return file.writeAsString('$content', mode: FileMode.APPEND);
  }
}

Подскажите, как сделать, чтобы в текстовый файл записывались сроки построчно :) простите за французский

1 ответ 1

0

Проблему решил использованием метода конкатенации строк и символов \n

    new RaisedButton(
      onPressed: () {
       String content = character.join("\n");
       CharacteristStorage().writeCaracter(content);

      },
      child: new Text('Сохранить в файл',
      style: new TextStyle(fontSize: 16.0),),
    )

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.