{"id":13481294,"url":"https://github.com/brianegan/flutter_redux","last_synced_at":"2025-04-13T20:44:45.193Z","repository":{"id":37382126,"uuid":"112011137","full_name":"brianegan/flutter_redux","owner":"brianegan","description":"A library that connects Widgets to a Redux Store","archived":false,"fork":false,"pushed_at":"2023-04-06T02:42:49.000Z","size":7982,"stargazers_count":1656,"open_issues_count":17,"forks_count":215,"subscribers_count":29,"default_branch":"master","last_synced_at":"2025-04-13T20:44:38.729Z","etag":null,"topics":["dart","dartlang","flutter","redux"],"latest_commit_sha":null,"homepage":null,"language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/brianegan.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-11-25T14:48:13.000Z","updated_at":"2025-04-11T08:15:36.000Z","dependencies_parsed_at":"2024-01-05T21:04:46.276Z","dependency_job_id":null,"html_url":"https://github.com/brianegan/flutter_redux","commit_stats":{"total_commits":77,"total_committers":20,"mean_commits":3.85,"dds":0.2727272727272727,"last_synced_commit":"4f32df61db8a140351e2c75d1154208bee7a21c0"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brianegan%2Fflutter_redux","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brianegan%2Fflutter_redux/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brianegan%2Fflutter_redux/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brianegan%2Fflutter_redux/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/brianegan","download_url":"https://codeload.github.com/brianegan/flutter_redux/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248782280,"owners_count":21160716,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dart","dartlang","flutter","redux"],"created_at":"2024-07-31T17:00:50.546Z","updated_at":"2025-04-13T20:44:45.167Z","avatar_url":"https://github.com/brianegan.png","language":"Dart","readme":"# flutter_redux\n\n[![Build Status](https://travis-ci.org/brianegan/flutter_redux.svg?branch=master)](https://travis-ci.org/brianegan/flutter_redux)  [![codecov](https://codecov.io/gh/brianegan/flutter_redux/branch/master/graph/badge.svg)](https://codecov.io/gh/brianegan/flutter_redux)\n\nA set of utilities that allow you to easily consume a\n[Redux](https://pub.dartlang.org/packages/redux) Store to build Flutter Widgets.\n\nThis package supports null-safety and is built to work with\n[Redux.dart](https://pub.dartlang.org/packages/redux) 5.0.0+ and Flutter 3+.\n\n## Redux Widgets \n\n  * `StoreProvider` - The base Widget. It will pass the given Redux Store to all descendants that request it.\n  * `StoreBuilder` - A descendant Widget that gets the Store from a `StoreProvider` and passes it to a Widget `builder` function.\n  * `StoreConnector` - A descendant Widget that gets the Store from the nearest `StoreProvider` ancestor, converts the `Store` into a `ViewModel` with the given `converter` function, and passes the `ViewModel` to a `builder` function. Any time the Store emits a change event, the Widget will automatically be rebuilt. No need to manage subscriptions!\n\n## Examples\n\n  * [Simple example](https://github.com/brianegan/flutter_redux/tree/master/example/counter) - a port of the standard \"Counter Button\" example from Flutter\n  * [Github Search](https://github.com/brianegan/flutter_redux/tree/master/example/github_search) - an example of how to search as a user types, demonstrating both the Middleware and Epic approaches.\n  * [Todo app](https://github.com/brianegan/flutter_architecture_samples/tree/master/redux) - a more complete example, with persistence, routing, and nested state.\n  * [Timy Messenger](https://github.com/janoodleFTW/timy-messenger) - large open source app that uses flutter_redux together with Firebase Firestore.\n  \n### Companion Libraries\n  * [flipperkit_redux_middleware](https://pub.dartlang.org/packages/flipperkit_redux_middleware) - Redux Inspector (use [Flutter Debugger](https://github.com/blankapp/flutter-debugger)) for Flutter Redux apps\n  * [flutter_redux_dev_tools](https://pub.dartlang.org/packages/flutter_redux_dev_tools) - Time Travel Dev Tools for Flutter Redux apps\n  * [redux_persist](https://github.com/Cretezy/redux_persist) - Persist Redux State   \n  * [flutter_redux_navigation](https://github.com/flutterings/flutter_redux_navigation) - Use redux events for navigation\n  * [flutter_redux_gen](https://marketplace.visualstudio.com/items?itemName=BalaDhruv.flutter-redux-gen) - VS Code Extension to generate redux code.\n \n## Usage\n\nLet's demo the basic usage with the all-time favorite: A counter example!\n\nNote: This example requires flutter_redux 0.4.0+ and Dart 2! If you're using\nDart 1, [see the old\nexample](https://github.com/brianegan/flutter_redux/blob/eb4289795a5a70517686ccd1d161abdb8cc08af5/example/lib/main.dart).\n\n```dart\nimport 'package:flutter/material.dart';\nimport 'package:flutter_redux/flutter_redux.dart';\nimport 'package:redux/redux.dart';\n\n// One simple action: Increment\nenum Actions { Increment }\n\n// The reducer, which takes the previous count and increments it in response\n// to an Increment action.\nint counterReducer(int state, dynamic action) {\n  return action == Actions.Increment ? state + 1 : state;\n}\n\nvoid main() {\n  // Create your store as a final variable in the main function or inside a\n  // State object. This works better with Hot Reload than creating it directly\n  // in the `build` function.\n  final store = Store\u003cint\u003e(counterReducer, initialState: 0);\n\n  runApp(FlutterReduxApp(\n    title: 'Flutter Redux Demo',\n    store: store,\n  ));\n}\n\nclass FlutterReduxApp extends StatelessWidget {\n  final Store\u003cint\u003e store;\n  final String title;\n\n  FlutterReduxApp({Key key, this.store, this.title}) : super(key: key);\n\n  @override\n  Widget build(BuildContext context) {\n    // The StoreProvider should wrap your MaterialApp or WidgetsApp. This will\n    // ensure all routes have access to the store.\n    return StoreProvider\u003cint\u003e(\n      // Pass the store to the StoreProvider. Any ancestor `StoreConnector`\n      // Widgets will find and use this value as the `Store`.\n      store: store,\n      child: MaterialApp(\n        theme: ThemeData.dark(),\n        title: title,\n        home: Scaffold(\n          appBar: AppBar(title: Text(title)),\n          body: Center(\n            child: Column(\n              mainAxisAlignment: MainAxisAlignment.center,\n              children: [\n                // Connect the Store to a Text Widget that renders the current\n                // count.\n                //\n                // We'll wrap the Text Widget in a `StoreConnector` Widget. The\n                // `StoreConnector` will find the `Store` from the nearest\n                // `StoreProvider` ancestor, convert it into a String of the\n                // latest count, and pass that String  to the `builder` function\n                // as the `count`.\n                //\n                // Every time the button is tapped, an action is dispatched and\n                // run through the reducer. After the reducer updates the state,\n                // the Widget will be automatically rebuilt with the latest\n                // count. No need to manually manage subscriptions or Streams!\n                StoreConnector\u003cint, String\u003e(\n                  converter: (store) =\u003e store.state.toString(),\n                  builder: (context, count) {\n                    return Text(\n                      'The button has been pushed this many times: $count',\n                      style: Theme.of(context).textTheme.display1,\n                    );\n                  },\n                )\n              ],\n            ),\n          ),\n          // Connect the Store to a FloatingActionButton. In this case, we'll\n          // use the Store to build a callback that will dispatch an Increment\n          // Action.\n          //\n          // Then, we'll pass this callback to the button's `onPressed` handler.\n          floatingActionButton: StoreConnector\u003cint, VoidCallback\u003e(\n            converter: (store) {\n              // Return a `VoidCallback`, which is a fancy name for a function\n              // with no parameters and no return value. \n              // It only dispatches an Increment action.\n              return () =\u003e store.dispatch(Actions.Increment);\n            },\n            builder: (context, callback) {\n              return FloatingActionButton(\n                // Attach the `callback` to the `onPressed` attribute\n                onPressed: callback,\n                tooltip: 'Increment',\n                child: Icon(Icons.add),\n              );\n            },\n          ),\n        ),\n      ),\n    );\n  }\n}\n```    \n\n## Purpose\n\nOne question that [reasonable people might\nask](https://www.reddit.com/r/FlutterDev/comments/6vscdy/a_set_of_utilities_that_allow_you_to_easily/dm3ll7d/):\nWhy do you need all of this if `StatefulWidget` exists?\n\nMy advice is the same as the original Redux.JS author: If you've got a simple\napp, use the most straightforward thing possible. In Flutter, `StatefulWidget` is perfect\nfor a simple counter app.\n\nHowever, say you have the more complex app, such as an E-commerce app with a\nShopping Cart. The Shopping Cart should appear on multiple screens in your app\nand should be updated by many different types of Widgets on those different\nscreens (An \"Add Item to Cart\" Widget on all your Product Screens, \"Remove Item\nfrom Cart\" Widget on the Shopping Cart Screen, \"Change quantity\" Widgets, etc.).\n\nAdditionally, you want to test this logic, as it's the core business\nlogic to your app!\n\nNow, in this case, you could create a Testable `ShoppingCart` class as a\nSingleton or Create a Root `StatefulWidget` that passes the `ShoppingCart `*Down\nDown Down* through your widget hierarchy to the \"add to cart\" or \"remove from\ncart\" Widgets.\n\nSingletons can be problematic for testing, and Flutter doesn't have a great\nDependency Injection library (such as Dagger2) just yet, so I'd prefer to avoid\nthose.\n\n\tYet passing the ShoppingCart all over the place can get messy. It also means\nit's way harder to move that \"Add to Item\" button to a new location b/c you'd\nneed up update the Widgets throughout your app that passes the state down.\n\nFurthermore, you'd need a way to Observe when the `ShoppingCart` Changes so you\ncould rebuild your Widgets when it does (from an \"Add\" button to an \"Added\"\nbutton, as an example).\n\nOne way to handle it would be to simply `setState` every time the `ShoppingCart`\nchanges in your Root Widget, but then your whole app below the RootWidget would\nbe required to rebuild as well! Flutter is fast, but we should be thoughtful about\nwhat we ask Flutter to rebuild!\n\nTherefore, `redux` \u0026 `redux_flutter` was born for more complex stories like this\none. It gives you a set of tools that allow your Widgets to `dispatch` actions\nin a naive way, then writes the business logic in another place that will take\nthose actions and update the `ShoppingCart` in a safe, testable way.\n\nEven more, once the `ShoppingCart` has been updated in the `Store`, the `Store`\nwill emit an `onChange` event. This lets you listen to `Store` updates and\nrebuild your UI in the right places when it changes! Now, you can separate your\nbusiness logic from your UI logic in a testable, observable way, without having\nto Wire up a bunch of stuff yourself!\n\nSimilar patterns in Android are the MVP Pattern or using Rx Observables to\nmanage a View's State.\n\n`flutter_redux` handles passing your `Store` down to all of your\ndescendant `StoreConnector` Widgets. If your State emits a change event, only\nthe `StoreConnector` Widgets and their descendants will be automatically rebuilt\nwith the latest State of the `Store`!\n\nThis allows you to focus on what your app should look like and how it should\nwork without thinking about all the glue code to hook everything together!\n\n### Contributors\n\n  * [Brian Egan](https://github.com/brianegan)\n  * [Chris Bird](https://github.com/chrisabird)\n","funding_links":[],"categories":["Dart","框架","状态管理","State management [🔝](#readme)","Frameworks"],"sub_categories":["Redux / ELM / 依赖注入","状态管理","State management","Redux / ELM / Dependency Injection"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrianegan%2Fflutter_redux","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbrianegan%2Fflutter_redux","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrianegan%2Fflutter_redux/lists"}