Member-only story
Flutter — How to Read text file from assets
A short tutorial on how to get text from a file in Flutter.
1. Create an asset folder
Create an assets folder in your project’s root folder. In Android Studio you can right click the Project outline and go to New > Directory.
You can create another subfolder for text files in assets
if you like. But if you do, you have to include the relative path in pubspec.yaml. See below.
2. Add text file to the new folder
You can just copy your text file into the assets
directory. The relative path of my_file.txt
, for example, would be assets/my_file.txt
.
3. Register the assets folder in pubspec.yaml
Open the pubspec.yaml file that is in the root of your project.
Add an assets subsection to the flutter section like this:
flutter:
assets:
- assets/my_file.txt
If you have multiple files that you want to include, then you can leave off the file name and just use the directory…