This is a re-implementation using fpdart and functional programming of the Open Meteo API from the flutter_weather app example in the bloc package.
The goal is to show a comparison between usual dart code and functional code written using fpdart.
The example is simple but comprehensive.
The Open Meteo API implementation is only 1 file. The original source is open_meteo_api_client.dart (copy of the bloc package implementation).
Inside lib/src/fpdart you can then find the refactoring using functional programming and fpdart:
- open_meteo_api_client_fpdart.dart: implementation of the Open Meteo API with
fpdart - location_failure.dart: failure classes for the
locationSearchrequest - weather_failure.dart: failure classes for the
getWeatherrequest
Also the test has been rewritten based on the fpdart refactoring:
- open_meteo_api_client_test.dart: Original Open Meteo API test implementation
- open_meteo_api_client_test_fpdart.dart: Testing for the new implementation using
fpdartand functional programming
TaskEither: Used instead ofFutureto make async request that may failEither: Used to validate the response from the API with either an error or a valid valueOption: Used to get values that may be missinglookupin aMap: getting a value by key in aMapmay return nothing if the key is not foundheadin aList: The list may be empty, so getting the first element (called "head") may return nothing