Code Monkey home page Code Monkey logo

infinite_list_bloc's Introduction

Infinite Scrollable List

In this project, I have developed a flutter app which implements bloc architecture to build an infinite scrollable list which loads data from REST API, jsonPlaceholder.

It followed following steps:

  1. Data Repository: @TODO need to move from BLoC.
  2. Model: Create post.dart model.
    1. This is simple PODO class with constructor and getter.
    2. uses equatable to compare two objects.
  3. Events: Fetched;
    1. Create abstract class, extend Equatable
    2. Create class for each of these events by extending abstract class.
  4. States: Loading, Loaded, Error.
    1. Create abstract class, extend Equatable
    2. Create class for each of these events by extending abstract class.
    3. Define variables to be affected in each State.
    4. No logic here, only scaffolds, factories.
  5. Bloc: A class which maps events and states classes.
    1. Provides initial state
    2. mapEventsToState
    3. Also, fetch posts using HTTP Client, this needs to be moved to repository, Step 0.
    4. Added, debouce events by transforming state before mapping, Overriding transform allows us to transform the Stream before mapEventToState is called. This allows for operations like distinct(), debounceTime(), etc... to be applied. Uses rxdart library.
  6. UI: Then main class:
    1. main() function calls the App() class.
    2. In App() widget, we use BlocProvider , provides instance of PostBloc to subtree.
    3. In child: HomePage(), we use BlocBuilder to return widget which updates on state change.
    4. Add fetched event to bloc. Whenever the user scrolls, we calculate how far away from the bottom of the page they are and if the distance is โ‰ค our _scrollThreshold we add a Fetch event in order to load more posts.
  7. Enhancements: We made app more robust by adding:
    1. Logging: We deligated bloc transitions by creating our own BlogDeligate. This logs all state transition with { current state, the event, and the next state } data.
    2. Optimization: We debounced the events. Overriding transform allows us to transform the Stream before mapEventToState is called. Using this we add debounceTime to events. This prevents spamming of API. 500ms is minimum time between each call.

Screenshots:

Screenshot 1 Screenshot 2
Initial State All posts loaded

App Structure:

  • lib
    • bloc
      • post_bloc.dart
      • post_event.dart
      • post_state.dart
      • simple_bloc_deligate.dart
    • models
      • post.dart
    • main.dart

This project was based on Bloc tutorial by @felangel.

Thank you!

infinite_list_bloc's People

Contributors

iyadavvaibhav avatar

Watchers

 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.