FLUTTER ECOSYSTEM

bizz84/open_weather_example_flutter

使用 OpenWeatherMap API 的 Flutter 天氣應用範例

open_weather_example_flutter 專案封面
Stars
311
Forks
74
最近推送(UTC)
2024年4月19日
專案狀態
未封存
Andrea Bizzotto GitHub avatar
GITHUB User

Andrea Bizzotto ↗

Flutter GDE ❖ Creator of codewithandrea.com ❖ YouTube: nnbd.me/yt ❖ Complete Dart Course: nnbd.me/dart

https://codewithandrea.comLondon官方網站 ↗
語言DartHTMLRubySwiftKotlinObjective-C

技術主題

使用的依賴

依賴清單 14 項

所在榜單

原始 README

以下為英文專案原文快照,最新內容請造訪 GitHub。

展開 / 收合專案 README

Flutter Weather App Example

An example Flutter weather app using the OpenWeatherMap API.

Flutter Weather App Preview

Related Tutorials

Supported Features

  • [x] Current weather (condition and temperature)
  • [x] 5-day weather forecast
  • [x] Search by city

App Architecture

The app is composed by three main layers.

Data Layer

The data layer contains a single HttpWeatherRepository that is used to fetch weather data from the OpenWeatherMap API.

The data is then parsed (using Freezed) and returned using type-safe entity classes (Weather and Forecast).

For more info about this, read this tutorial:

For more info about the project structure, read this:

Application Layer

This contains some providers that are used to fetch and cache the data from the HttpWeatherRepository.

// current city stored in the search box in the UI
final cityProvider = StateProvider<String>((ref) {
  return 'London';
});

// provider to fetch the current weather
final currentWeatherProvider =
    FutureProvider.autoDispose<WeatherData>((ref) async {
  final city = ref.watch(cityProvider);
  final weather =
      await ref.watch(weatherRepositoryProvider).getWeather(city: city);
  return WeatherData.from(weather);
});

// provider to fetch the hourly weather
final hourlyWeatherProvider =
    FutureProvider.autoDispose<ForecastData>((ref) async {
  final city = ref.watch(cityProvider);
  final forecast =
      await ref.watch(weatherRepositoryProvider).getForecast(city: city);
  return ForecastData.from(forecast);
});
Presentation Layer

This layer holds all the widgets, which fetch the data from the FutureProviders above and map the resulting AsyncValue objects to the appropriate UI states (data, loading, error).

Packages in use

About the OpenStreetMap weather API

The app shows data from the following endpoints:

Note: to use the API you'll need to register an account and obtain your own API key. This can be set via --dart-define or inside lib/src/api/api_keys.dart.

LICENSE: MIT