Coder Social home page Coder Social logo

hoc081098 / stream_loader Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 2.0 330 KB

🍂 A Flutter plugin for loading content asynchronously with Dart stream and RxDart. RxDart loader bloc. Reactive loader bloc. Simple reactive state management container - https://pub.dev/packages/stream_loader

Home Page: https://pub.dev/packages/stream_loader

License: MIT License

Kotlin 0.13% Swift 1.04% Objective-C 0.03% Dart 60.88% CMake 16.27% C++ 18.75% C 1.28% HTML 1.63%
flutter-bloc-pattern flutter-bloc flutter-bloc-rxdart flutter-bloc-patterns flutter-bloc-pattern-rxdart flutter-rxdart flutter-rx flutter-reactive flutter-stream stream-loader

stream_loader's Introduction

stream_loader alt text

  • A Flutter plugin for loading content asynchronously with Dart Stream and RxDart.
  • RxDart loader bloc.
  • Reactive loader bloc.
  • Simple reactive state management container.

Tests Build example Pub Build Status codecov License: MIT

Getting Started

In your flutter project, add the dependency to your pubspec.yaml

dependencies:
  ...
  stream_loader: <latest_version>

Examples

Usage

1. Model and api

abstract class Comment implements Built<Comment, CommentBuilder> { ... }

class Api {
  Stream<BuiltList<Comment>> getComments() { ... }
  Stream<Comment> getCommentBy({@required int id}) { ... }
}
final api = Api();

2. Create LoaderWidget load comments from api

import 'package:stream_loader/stream_loader.dart';

LoaderWidget<BuiltList<Comment>>(
  blocProvider: () => LoaderBloc(
    loaderFunction: api.getComments,
    refresherFunction: api.getComments,
    initialContent: <Comment>[].build(),
    logger: print,
  ),
  messageHandler: (context, message, bloc) {
    message.fold(
      onFetchFailure: (error, stackTrace) => context.snackBar('Fetch error'),
      onFetchSuccess: (_) {},
      onRefreshSuccess: (data) => context.snackBar('Refresh success'),
      onRefreshFailure: (error, stackTrace) => context.snackBar('Refresh error'),
    );
  },
  builder: (context, state, bloc) {
    if (state.error != null) {
      return ErrorWidget(error: state.error);
    }
    if (state.isLoading) {
      return LoadingWidget();
    }
    return RefreshIndicator(
      onRefresh: bloc.refresh,
      child: CommentsListWidget(comments: state.content),
    );
  }
);

3. Create LoaderWidget load comment detail from api

import 'package:stream_loader/stream_loader.dart';

final Comment comment;
final loadDetail = () => api.getCommentBy(id: comment.id);

LoaderWidget<Comment>(
  blocProvider: () => LoaderBloc(
    loaderFunction: loadDetail,
    refresherFunction: loadDetail,
    initialContent: comment,
    logger: print,
  ),
  messageHandler: (context, message, bloc) {
    message.fold(
      onFetchFailure: (_, __) {},
      onFetchSuccess: (_) {},
      onRefreshFailure: (_, __) {},
      onRefreshSuccess: (_) => context.snackBar('Refresh success'),
    );
  },
  builder: (context, state, bloc) {
    return RefreshIndicator(
      onRefresh: bloc.refresh,
      child: CommentDetailWidget(comment: state.content),
    );
  },
);

Note: Can use LoaderBloc without LoaderWidget easily

class _CommentsState extends State<Comments> {
  LoaderBloc<BuiltList<Comment>> bloc;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();

    bloc ??= LoaderBloc(
      loaderFunction: api.getComments,
      refresherFunction: api.getComments,
      initialContent: <Comment>[].build(),
      logger: print,
    )..fetch();
  }

  @override
  void dispose() {
    bloc.dispose();
    super.dispose();
  }
  
  @override
  Widget build(BuildContext context) {
    return StreamBuilder<LoaderState<BuiltList<Comment>>>(
      stream: bloc.state$,
      initialData: bloc.state$.value, // <- required because bloc.state$ does not replay the latest value
      builder: (context, snapshot) {
        final state = snapshot.data;
        
        if (state.error != null) {
          return ErrorWidget(error: state.error);
        }
        if (state.isLoading) {
          return LoadingWidget();
        }
        return RefreshIndicator(
          onRefresh: bloc.refresh,
          child: CommentsListWidget(comments: state.content),
        );
      }
    );
  }
}

Change flatten behavior of loaderFunction and refresherFunction.

  • Default behavior of loaderFunction is FlattenStrategy.latest (uses switchMap).
  • Default behavior of refreshFlatMapPolicy is FlattenStrategy.first, (uses exhaustMap).
  • To change them, passing your value to LoaderBloc constructor
LoaderBloc(
  ...,
  loaderFlattenStrategy: FlattenStrategy.concat, // asyncExpand
  refreshFlattenStrategy: FlattenStrategy.latest, // switchMap
);

License

MIT License

Copyright (c) 2020-2022 Petrus Nguyễn Thái Học

stream_loader's People

Contributors

dependabot[bot] avatar hoc081098 avatar renovate-bot avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

stream_loader's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): update actions/checkout action to v4
  • chore(deps): update actions/setup-java action to v4
  • chore(deps): update actions/upload-artifact action to v4
  • chore(deps): update codecov/codecov-action action to v4
  • chore(deps): update dependency flutter_lints to v4
  • 🔐 Create all rate-limited PRs at once 🔐

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/build-example.yml
  • actions/checkout v3
  • actions/setup-java v3
  • subosito/flutter-action v2.6.1
  • actions/upload-artifact v3
.github/workflows/dart.yml
  • actions/checkout v3
  • subosito/flutter-action v2.6.1
  • codecov/codecov-action v3.1.0
.github/workflows/remove-old-artifacts.yml
  • c-hive/gha-remove-artifacts v1
gradle
example/android/gradle.properties
example/android/settings.gradle
example/android/build.gradle
  • com.android.tools.build:gradle 7.1.2
  • org.jetbrains.kotlin:kotlin-gradle-plugin 1.6.10
  • org.jetbrains.kotlin:kotlin-stdlib-jdk7 1.6.10
example/android/app/build.gradle
gradle-wrapper
example/android/gradle/wrapper/gradle-wrapper.properties
  • gradle 7.4
pub
example/pubspec.yaml
  • flutter
  • cupertino_icons ^1.0.5
  • http ^0.13.4
  • built_value ^8.4.0
  • flutter_provider ^2.1.0
  • built_collection ^5.1.1
  • flutter_lints ^2.0.1
  • built_value_generator ^8.4.0
  • build_runner ^2.2.0
  • quiver ^3.1.0
  • dart >=2.17.0 <3.0.0
  • flutter >=3.0.0
pubspec.yaml
  • disposebag ^1.5.1
  • rxdart_ext ^0.2.5
  • flutter
  • mockito ^5.2.0
  • flutter_lints ^2.0.1
  • build_runner ^2.2.0
  • analyzer ^4.1.0
  • code_builder ^4.1.0
  • dart >=2.17.0 <3.0.0
  • flutter >=3.0.0

  • Check this box to trigger a request for Renovate to run again on this repository

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.