Search
 
SCRIPT & CODE EXAMPLE
 

DART

dart how to write a a file

Future<File> writeCounter(int counter) async {
  final file = await _localFile;

  // Write the file
  return file.writeAsString('$counter');
}
Comment

dart write to file

main() async {
  List letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"];
  File file = new File("Letters.txt");
  for (int i = 0; i < 10; i++) {
    await file.writeAsString("${letters[i]}", mode: FileMode.append);
  }
}
Comment

flutter write file

Simple solution, you can make the file structure more complex
Package: path_provider


import 'dart:convert';
import 'dart:io';

import 'package:path_provider/path_provider.dart';

abstract class OurDataStorage {
  static Future<String> get documentsDirectoryPath async {
    return (await getApplicationDocumentsDirectory()).path;
  }

  static Future<String> get temporaryDirectoryPath async {
    return (await getTemporaryDirectory()).path;
  }

  static Future writeDocument(String document, Map<String, dynamic> data) async {
    await File("${await documentsDirectoryPath}/$document.json").writeAsString(jsonEncode(data));
  }

  static Future writeTemporary(String document, Map<String, dynamic> data) async {
    await File("${await temporaryDirectoryPath}/$document.json").writeAsString(jsonEncode(data));
  }

  static Future<Map<String, dynamic>> readDocument(String document) async {
    return jsonDecode(await File("${await documentsDirectoryPath}/$document.json").readAsString());
  }

  static Future<Map<String, dynamic>> readTemporary(String document) async {
    return jsonDecode(await File("${await temporaryDirectoryPath}/$document.json").readAsString());
  }

  static Future updateDocument(String document, Map<String, dynamic> data) async {
    final fileData = await readDocument(document);
    data.forEach((key, value) => fileData[key] = value);
    await writeDocument(document, fileData);
  }

  static Future updateTemporary(String document, Map<String, dynamic> data) async {
    final fileData = await readTemporary(document);
    data.forEach((key, value) => fileData[key] = value);
    await writeTemporary(document, fileData);
  }

  static Future deleteDocument(String document) async {
    await File("${await documentsDirectoryPath}/$document.json").delete();
  }

  static Future deleteTemporary(String document) async {
    await File("${await temporaryDirectoryPath}/$document.json").delete();
  }

  static Future clearDocuments() async {
    final directory = await getApplicationDocumentsDirectory();
    await directory.delete(recursive: true);
    await directory.create();
  }

  static Future clearTemporary() async {
    final directory = await getTemporaryDirectory();
    await directory.delete(recursive: true);
    await directory.create();
  }
}
Comment

PREVIOUS NEXT
Code Example
Dart :: how to launch url in flutter web 
Dart :: flutter add value to list<map<string, int 
Dart :: listview inside column flutter 
Dart :: flutter refresh page 
Dart :: check if string contain number dart flutter 
Dart :: transparent appbar flutter 
Dart :: flutter check type of object 
Dart :: dart and flutter 
Dart :: flutter back button with data 
Dart :: children vs child dart 
Dart :: imageprovider flutter 
Dart :: flutter int max value 
Dart :: dart else if 
Dart :: flutter appbar remove padding 
Dart :: app bar textStyle flutter 
Dart :: elevated Button Theme background color in flutter 
Dart :: position of item in array dart 
Dart :: flutter auto size text 
Dart :: flutter array filter 
Dart :: Autocomplete Widget in Flutter 
Dart :: dart test expect assert fail 
Dart :: flutter icondata 
Dart :: flutter api service example 
Dart :: flutter firebase 
Dart :: string null dart 
Dart :: how to replace string character in dart 
Dart :: install fvm in flutter using pub package 
Dart :: onpressed flutter calculate 
Dart :: dart set union 
Dart :: flutter sliver app bar remove top padding 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =