Make API calls in Flutter using TDD approach

All code is guilty until proven innocent

Waleed Arshad
CodeChai

--

Errors reveal the character of a code — Anonymous

Today we will see how we can efficiently make API calls in Flutter using Test-Driven Approach. We will be using a sample user API publicly available at JSON placeholder.

IMPORTANT: If you want to know the approach, you will have to follow things from the beginning to the end. Jumping at the end and copying the code will make no difference because it is simply 4 lines of an API call. The actual essence lies in the understanding of TDD Philosophy.

So without wasting your time and my words, we jump to creating a new test file under the test folder and write an empty test inside it (It is assumed that you have a sample Flutter app already up and working).

void main () {
test("api test", () async {

});
}

The TDD approach

In simple words TDD says the following:

  1. Keep writing code only until you get an error (compile-time errors are errors too)
  2. Once you get an error, write some code only to fix that specific error.
  3. Write more code only until an error occurs.
  4. REPEAT

--

--

Waleed Arshad
CodeChai

Writer || Tech philosophy || Senior Flutter Engineer || Google Developer Expert