Code Monkey home page Code Monkey logo

freezed_weather's Introduction

freezed_weather

A rewrite of Bloc tutorial: Flutter Weather Tutorial using freezed.

  • Bloc is used instead of Cubit (Cubit vs Bloc)
    • Bloc provides better traceability of what's happening.
      • onTransition shows the event that triggers the new state
    • Bloc allows advanced event transformations
    • However, bloc requires more detailed implementation. freezed code generation could be used to reduce the amount of coding.
  • freezed is used to compare data models. equatable is not used anymore.
    • A @freezed class comes with generated copyWith implementation, which is not prone to human-made errors (e.g., forget to add a variable into prop list, which is the mistake I've made).
  • Use wttr.in as the remote data provider.
    • MetaWeather API is not available since May 2022.
    • Current workaround
      • A new package wttr_in_api, which implements a client for wttr.in, is created.
      • WeatherRepository is an abstract class now.
      • The previous WeatherRepository becomes MetaWeatherWeatherRepository.
      • A new WttrInWeatherRepository is created to use wttr_in_api.
      • WeatherRepository needs to be instantiated through WeatherRepository.instance() now.

Notable changes

@freezed data models

  • copyWith, == and toString implementation

  • Properties can be null

    Previously, Weather.empty is provided in initial WeatherState because equatable does not allow null in props. freezed handles null properly so we can just use null as weather in the state when weather is not available yet (e.g., in the initial state). Both work fine, it's only a matter of choice so empty is kept in lib/weather/models/weather.dart.

@freezed event & state for bloc

Persist only the loaded state

In the original tutorial, all states are cached. Imagine the scenario: When an error happens, the user terminates the app and reopens it. Guess what? The error state is served immediately!

The bloc is working properly but the user experience is not optimal. WeatherBloc.toJson is modified to only cache the successfully loaded state. When the user opens the app, it always shows that last loaded weather (or the initial state if nothing happens yet). Again, it's a matter of choice of when to cache the states.

Changelog

After MetaWeather is down

A new package wttr_in_api, which implements a client for wttr.in, is created. The layered structure makes it possible to switch between different API providers. WeatherRepository is refactored to an abstract class to reflect this.

You can use tag 2.0 to fetch the previous project, which uses only the MetaWeather API.

After Flutter 3.0

This tutorial has been rewritten after the releases of Flutter 3.0, freezed 2.0.3, and hydrated_bloc 9.0.0-dev.2.

It won't work on previous versions. You can use tag 1.0 to fetch the older scripts.

freezed_weather's People

Contributors

yh-luo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.