Search
 
SCRIPT & CODE EXAMPLE
 

DART

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 run dart code in vscode 
Dart :: dart main 
Dart :: image not shoing when i use network image,flutter 
Dart :: search functionality dart 
Dart :: flutter fittedbox max value 
Dart :: floting action button tooltip 
Dart :: crossaxisalignment.stretch row in column flutter 
Dart :: flutter type check 
Dart :: create extention in dart 
Dart :: how to create camera icon in flutter dev 
Dart :: creating a clas in dart 
Dart :: flutter webview platform._operatingsystem 
Dart :: flutter sliver app bar remove top padding 
Dart :: flutter elif 
Dart :: convert string date in Format yyyyMMddHHmmss to DateTime dart 
Dart :: convert seconds to minutes in Dart 
Swift :: settimeout in swift 
Swift :: swift hide navigation bar 
Swift :: print document directory path swift 
Swift :: remove checkmark when selecting the cell again swift 5 
Swift :: swift http request 
Swift :: fizzbuzz in swift 
Swift :: swift filter dictionary 
Swift :: add top corner radius swift 
Swift :: IOS create UIAlertViewController programmatically 
Swift :: tuple swift 
Swift :: hex color extension swift 
Swift :: swift change label text 
Swift :: how to change background color of stackview swift 
Swift :: how to bold 1 word swift 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =