US Office

1176 Shadeville Rd, Crawfordville Florida 32327, USA

 +1 (850) 780-1313

India Office

Office No 405, Kabir Shilp, Opp. Kansar Hotel, Opp. Landmark, Kudasan, Gandhinagar, Gujarat 382421

[email protected]

How to Load File For Testing In Flutter??

How to Load File For Testing In Flutter

How to Load File For Testing In Flutter??

The file can be read from the directory relative to the dart script file. So in this article, we will go through how to Load File For Testing In Flutter.

Are you ready for the same? Let’s start right away!!

How to Load File For Testing In Flutter?

First, create a folder that lives in the same directory as your tests are. For example, I created a folder called test_resources.



Then, let’s say we have the following JSON file for testing purposes.


  "contacts": [
      "id": 1,
      "name": "Seth Ladd"
      "id": 2,
      "name": "Eric Seidel"


We could use it for our test like so:

import 'dart:convert';
import 'dart:io';
import 'package:flutter_test/flutter_test.dart';

void main() {
  test('Load a file', () async {
    final file = new File('test_resources/contacts.json');
    final json = jsonDecode(await file.readAsString());
    final contacts = json['contacts'];

    final seth = contacts.first;
    expect(seth['id'], 1);
    expect(seth['name'], 'Seth Ladd');

    final eric = contacts.last;
    expect(eric['id'], 2);
    expect(eric['name'], 'Eric Seidel');

So you can just put your test files in two places I know it is not a good practice to have the same data in two places.

Let’s say I want to have a data.json file to be opened in code under tests with final file = File(dataFilename), then dataFilename will be ‘test_resources/data.json‘.

If a test is executed from Android Studio, test_resources is a folder in the project root. If tests are executed from the command line test_resources is a folder in the test folder.

You can also follow this GitHub link

String fixture(String name) {
   var dir = Directory.current.path;
   if (dir.endsWith('/test')) {
     dir = dir.replaceAll('/test', '');
   return File('$dir/test/fixtures/$name').readAsStringSync();


Thanks for being with us on a Flutter Journey !!!

So in this article, we have been through how to Load File For Testing In Flutter.

Keep Learning !!! Keep Fluttering !!! Stay Connected !!!

Do Let us know if you are still facing doubts about Flutter Development!! We would love to assist you ūüôā

Flutter Agency is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget Guide, Flutter Projects, Code libs and etc.

Flutter Agency is one of the most popular online portals dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge of Flutter.

Post a Comment