Reading a text file from assets in Flutter

Suragch
2 min readJun 4, 2019

This is a repost of an answer I wrote on Stack Overflow.

In this brief article I’ll show all the steps for how to load a text file from assets into your Flutter app.

Create an assets 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.

Add your 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.

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

--

--