rxdart_bloc

Last updated:

0 purchases

rxdart_bloc Image
rxdart_bloc Images
Add to Cart

Description:

rxdart bloc

If you are working on a complex project you might be challenged to build a highly interactive UI or
a heavy business logic in a combination with the consumption of various data sources such as REST
APIs, Web Socket, Secured Storage, Shared Preferences, etc. To achieve this, you might need a
sophisticated architecture that facilitates your work during product development.
Screenshot #



SUCCESS
ERROR
ARCH















Getting started #
With null-safety
rxdart_bloc: ^1.0.14
copied to clipboard
Add the dependency to your project and start using rxdart_bloc #
Importing the package #
import 'package:rxdart_bloc/rxdart_bloc.dart';
copied to clipboard
Usage #
To use this plugin, add rxdart_bloc as a dependency in your pubspec.yaml file.
Example #
Here are an example that show you how to use this plugin.
main.dart #
import 'package:flutter/material.dart';
import 'package:rxdart_bloc/bloc_provider.dart';

import 'products/bloc/products_bloc.dart';
import 'products/view/products_view.dart';

void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter rxdart_bloc example',
home: BlocProvider(bloc: ProductsBloc(), child: const ProductsView()),
);
}
}
copied to clipboard
products_view.dart #

import 'package:flutter/material.dart';
import 'package:rxdart_bloc/rxdart_bloc.dart';

import '../bloc/products_bloc.dart';
import '../model/products_response_model.dart';
import 'widget/products_item_widget.dart';

class ProductsView extends StatefulWidget {
const ProductsView({Key? key}) : super(key: key);

@override
State<ProductsView> createState() => _ProductsViewState();
}

class _ProductsViewState extends State<ProductsView> {
late ProductsBloc _bloc;

@override
void initState() {
bloc = BlocProvider.of<ProductsBloc>(context);
bloc.getProducts();
super.initState();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Products'),),
body: StreamBuilder<GetAllProductsResponseModel>(
stream: bloc.successStream,
builder: (context, snapshot) {
return StreamingResult(
subject: bloc.requestStateSubject,
successWidget: ListView.builder(
itemCount: snapshot.data?.products?.length,
padding: const EdgeInsets.all(16),
itemBuilder: (context, index) {
return ProductsItemWidget(
content: snapshot.data?.products?[index],
);
}),
retry: () =>
bloc.getProducts(),
);
}
),
);
}

}
copied to clipboard
products_bloc.dart #
import 'package:rxdart_bloc/rxdart_bloc.dart';
import '../../../network/network.dart';
import '../../models/error_model.dart';
import '../model/products_response_model.dart';
import '../repo/products_repo.dart';

class ProductsBloc extends BaseBloc
with RxdartBlocState<GetAllProductsResponseModel, ErrorModel> {
BehaviorSubject<RequestState> requestStateSubject = BehaviorSubject.seeded(
RequestState(status: RequestStatus.init, message: 'INITIAL'));
final ProductsRepo _countriesRepo =
ProductsRepo(Network('https://dummyjson.com/'));

Future getProducts() async {
requestStateSubject.sink
.add(RequestState(status: RequestStatus.loading, message: 'LOADING'));

var model = await _countriesRepo.getProducts();
if (model is GetAllProductsResponseModel) {
super.successSubject.sink.add(model);
requestStateSubject.sink
.add(RequestState(status: RequestStatus.success, message: 'SUCCESS'));
}
if (model is ErrorModel) {
super.errorSubject.sink.add(model);
requestStateSubject.sink.add(RequestState(
status: RequestStatus.error, message: model.message ?? ''));
}
}

@override
void dispose() {
requestStateSubject.close();
}
}
copied to clipboard
products_repo_interface.dart #
import 'package:rxdart_bloc/base_model.dart';

abstract interface class ProductsRepoInterface {
Future<BaseModel> getProducts();
}
copied to clipboard
products_repo.dart #
import 'package:rxdart_bloc/base_model.dart';
import '../../../network/network.dart';
import '../../models/error_model.dart';
import '../model/products_response_model.dart';
import 'products_repo_interface.dart';

class ProductsRepo implements ProductsRepoInterface {
final Network _network;
ProductsRepo(this._network);

@override
Future<BaseModel> getProducts() async {
try {
var response =
await _network.request(HttpMethod.get, endpoint: 'products');
return GetAllProductsResponseModel.fromJson(response?.data);
} catch (e) {
return ErrorModel.fromJson(e as dynamic);
}
}
}
copied to clipboard
rxdart_bloc widgets #
BlocProvider #
BlocProvider is a Flutter widget which provides a bloc to its children.

BlocProvider(bloc: ProductsBloc(), child: const ProductsView());

copied to clipboard
MultiBlocProvider #
MultiBlocProvider is a Flutter widget which provides a multi blocs to their children.
MultiBlocProvider<List<BaseBloc>>(
blocs: [
Bloc1(),
Bloc2(),
], child: const ProductsView()
);
copied to clipboard
StreamingResult #
StreamingResult is a Flutter widget which returns the result of request streaming. you can provide your custom initWidget, successWidget, emptyWidget, errorWidget, retry function

StreamingResult(
subject: _bloc.requestStateSubject,
// initWidget: const Center(child: Text('Initial')),
successWidget: ListView.builder(
itemCount: snapshot.data?.products?.length,
padding: const EdgeInsets.all(16),
itemBuilder: (context, index) {
return ProductsItemWidget(content: snapshot.data?.products?[index]);
}),
// emptyWidget: const Center(child: Text('Empty')),
// errorWidget: const Center(child: Text('Error')),
retry: () => _bloc.getProducts(),
);
copied to clipboard
Declare bloc #
late ProductsBloc _bloc;

copied to clipboard
Initialize bloc #
@override
void initState() {
_bloc = BlocProvider.of<ProductsBloc>(context);
_bloc.getProducts();
super.initState();
}
copied to clipboard
Improve #
Help me by reporting bugs, submit new ideas for features or anything else that you want to share.

Just write an issue on GitHub. ✏️
And don't forget to hit the like button for this package ✌️

More #
Check out my other useful packages on pub.dev

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.