'Flutter: Can't edit and access text file through app anymore after restarting Program for the first time

so basically I am trying to build a simple app in Flutter which opens a text file, copies its content into a Text Form Widget, lets the user edit the text in the Widget, and replaces the old content in the text file with the new content in the Text Field. Now, the saving part doesn't seem to work, as I always have a blank Field after starting the app. While debugging I noticed, that the content of the text file only changes, with press on the "save" button, on the first time I start the app, so basically when I create the text file (I experimented by creating different text files). Now, once I reload the app and print the content of the text file, it prints the last text I've written into the Text Field before closing it the first time, so it seems as if I no longer have access to the file. Does someone have an idea why that could be? If it helps, I've added the following permissions in Android Manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION"/>
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />

Thank you in advance.

import 'dart:ffi';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';
import 'package:path_provider/path_provider.dart';
import "./main.dart";

class NewScreen extends StatefulWidget {
  const NewScreen({Key? key}) : super(key: key);

  @override
  State<NewScreen> createState() => NewScreenState();
}


class NewScreenState extends State<NewScreen> {
  TextEditingController myController = TextEditingController();

  

  read() async {
    try {
      final directory = await getApplicationDocumentsDirectory();
      final file = File('${directory.path}/text_vault.txt');
      myController = TextEditingController(text: await file.readAsString());
    } catch (e) {
      print("Couldn't read file");
    }
  }

  save() async {
    final directory = await getApplicationDocumentsDirectory();
    final file = File('${directory.path}/text_vault.txt');
    final text = myController.text;
    await file.writeAsString(text);
    print('saved ' + myController.text);
  }


  @override
  Widget build(BuildContext context) {
    Permission.storage.request();
    read();
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Container(
            width: double.infinity,
            child: Text(
              "Vault",
              style: TextStyle(fontSize: 24),
              textAlign: TextAlign.center,
            ),
          ),
        ),
        body: Column(
          children: [
            Expanded(
              child: SingleChildScrollView(
                scrollDirection: Axis.vertical,
                child: TextFormField(
                  controller: myController,
                decoration: InputDecoration(border: InputBorder.none),
                style: TextStyle(fontSize: 26),
                showCursor: true,
                maxLines: null,
                ),
                
              ),
            ),
            
          ],
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: save,
          child: Icon(Icons.save),
        ),
      ),
    );
  }
}


Sources

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

Source: Stack Overflow

Solution Source