Saving Data in Flutter

Jan 31 2024 · Dart 3, Flutter 3.10, Visual Studio Code

Part 3: Reading & Writing Files

14. Creating the File Screen

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 13. Creating the File List Screen (Part 2) Next episode: 15. Polishing the App

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Now that we have the screen that contains the list of files in the documents directory of our app, we need a screen to read and write the file content. We will call this screen when users want to create a new file, and when they want to edit an existing one.

final String? fileName; 
const FileScreen({this.fileName, super.key}); 
final TextEditingController _fileNameController = TextEditingController(); 
final TextEditingController _contentController = TextEditingController(); 
final FileHelper _fileHelper = FileHelper(); 
String _statusMessage = ''; 
final content = await _fileHelper.readFile(_fileNameController.text); 
 Future<void> _deleteFile() async { 
    await _fileHelper.deleteFile(_fileNameController.text); 
    _contentController.clear(); 
    setState(() { 
      _statusMessage = 'File deleted.'; 
    }); 
  } 
void dispose() { 
  _fileNameController.dispose(); 
  _contentController.dispose(); 
  super.dispose(); 
} 
@override 
void initState() { 
  super.initState(); 
  if (widget.fileName != null) { 
    _fileNameController.text = widget.fileName!; 
    _readFile(); 
  } 
} 
title: Text(widget.fileName ?? 'New File'), 
padding: const EdgeInsets.all(16), 
crossAxisAlignment: CrossAxisAlignment.start, 
TextField( 
  controller: _fileNameController, 
  decoration: const InputDecoration(labelText: 'File Name'), 
), 
void _openFileScreen({String? fileName}) { 
  Navigator.push<dynamic>( 
    context, 
    MaterialPageRoute<dynamic>( 
      builder: (context) => FileScreen(fileName: fileName), 
    ), 
  ).then((dynamic _) => setState(() {})); 
}
floatingActionButton: FloatingActionButton( 
  onPressed: _openFileScreen, 
  child: const Icon(Icons.add) 
), 
onTap: () { 
  _openFileScreen(fileName: file.path.split('/').last); 
}