wsds 1.0.0 copy "wsds: ^1.0.0" to clipboard
wsds: ^1.0.0 copied to clipboard

Weanel Standardize Data Structre for loading current weather, weather warning and weather forecast

example/main.dart

import 'dart:convert';
import 'dart:io';

import 'package:weather_unit/weather_unit.dart';
import 'package:wsds/wsds.dart';

WeatherExpression resolveExpressionFromREST(String restExpression) {
  switch (restExpression) {
    case "sunny":
      return WeatherExpression.sun;
    case "cold":
      return WeatherExpression.cold;
    default:
      return WeatherExpression.sun_with_cloud;
  }
}

void main() {
  // Current weather
  final Map<String, dynamic> dummyCurrent =
      jsonDecode("{\"temp\": 25, \"humid\": 75, \"icon\": \"sunny\"}");

  print(CurrentWeather(
          Celsius(dummyCurrent["temp"]),
          Humidity(dummyCurrent["humid"]),
          resolveExpressionFromREST(dummyCurrent["icon"]))
      .toJSONString);

  // Warning
  final String dummyWarning =
      "[{\"warncode\": \"sampwarn\", \"warnName\": \"Sample warning\"}]";

  WeatherWarning warn = WeatherWarning([
    SingleWeatherWarning("sampwarn", "Sample warning which defined in WSDS",
        File("example/a.png"))
  ], (warndef) {
    List<IssuedWarning> issued = [];

    List<dynamic> warnREST = jsonDecode(dummyWarning);

    warnREST.forEach((w) {
      var containWarn =
          warndef.where((def) => def.warningCode == w["warncode"]).single;
      if (containWarn is GroupedWeatherWarning) {
        issued.add(
            IssuedWarning(warning: containWarn.findIGWarning(w["subwarn"])));
      } else if (containWarn is SingleWeatherWarning) {
        issued.add(IssuedWarning(warning: containWarn));
      }
    });

    return issued;
  });

  print(warn.toJSONString);

  // Forecast
  final String dummyForecast =
      "[{\"date\": \"2021-01-01\", \"minTemp\": 25, \"maxTemp\": 26, \"minHumid\": 50, \"maxHumid\": 99, \"icon\": \"sunny\"}, {\"date\": \"2021-01-02\", \"minTemp\": 20, \"maxTemp\": 21, \"minHumid\": 35, \"maxHumid\": 60, \"icon\": \"cold\"}]";

  List<dynamic> forecastREST = jsonDecode(dummyForecast);

  print(WeatherForecast(forecastREST
          .map((e) => ForecastInDate(
              DateTime.parse(e["date"]),
              Celsius(e["minTemp"]),
              Celsius(e["maxTemp"]),
              Humidity(e["minHumid"]),
              Humidity(e["maxHumid"]),
              resolveExpressionFromREST(e["icon"])))
          .toList())
      .toJSONString);
}
0
likes
150
points
1
downloads

Publisher

unverified uploader

Weekly Downloads

Weanel Standardize Data Structre for loading current weather, weather warning and weather forecast

Homepage
Repository (GitHub)

Documentation

API reference

License

Apache-2.0 (license)

Dependencies

intl, weather_unit

More

Packages that depend on wsds