Step 1 / 4
Define what, not how
An abstract class describes what the app needs: "give me today's weather for a city". Nothing about HTTP or JSON.
Depend on abstract interfaces, inject implementations, and swap in fakes for tests.
1abstract class WeatherRepository {2 Future<Weather> today(String city);3}45class ApiWeatherRepository implements WeatherRepository {6 ApiWeatherRepository(this._client);7 final http.Client _client;89 @override10 Future<Weather> today(String city) async {11 final res = await _client.get(Uri.https('api.example.com', '/weather', {'q': city}));12 return Weather.fromJson(jsonDecode(res.body) as Map<String, dynamic>);13 }14}1516class FakeWeatherRepository implements WeatherRepository {17 @override18 Future<Weather> today(String city) async => Weather.sunny(city);19}2021// Wire the real implementation once, at the top of the app22void main() => runApp(23 Provider<WeatherRepository>(24 create: (_) => ApiWeatherRepository(http.Client()),25 child: const WeatherApp(),26 ),27);
An abstract class describes what the app needs: "give me today's weather for a city". Nothing about HTTP or JSON.