Comments (12)
Hey hey, sorry about the delay! Came back to quite a bit of work :)
Here's the best solution I could think of:
- Create an Action that contains a
Completer
- Return the
action.completer.future
to theonRefresh
function of theRefreshIndicator
- Write a
Middleware
that listens for this action. It handles:
* Making an async call to a database or web api
* After the async call completes, does the normal thing: dispatches an action with some payload
* Also performsaction.completer.complete
. This will inform the RefreshIndicator that the async work is complete, and will hide the spinner.
Example here: https://github.com/brianegan/flutter_redux/blob/refresh-indicator-example/example/lib/main.dart#L10
I'll try to think if there's a good way to make this easier in some way... Maybe a generic action or middleware.
from flutter_redux.
I'm profusely apologetic @brianegan - I think that was a case of me working on this too late at night! Typo on my part caused a type error, but it is fixed and working. I'll re-close the issue. Next challenge - work out how to animate things whilst using redux...
from flutter_redux.
Heya @tanphathuynh -- yep, that's a standard type error, which Dart 2 is now more strict about. In this case, the completer
must now be a Completer<Null>
rather than a plain old Completer
(which is inferred as a Completer<dynamic>
.
from flutter_redux.
Ah, that's an interesting one. Thanks for writing in i'm on vacation for a few days, but I'll take a look at this and bit more carefully when I'm back in action early next week to see if I can make something easy to use :)
from flutter_redux.
That's great, thanks @brianegan . Enjoy your vacation!
from flutter_redux.
Brian,
This worked brilliantly and was not difficult to implement (thanks to your great example!). I'm new to dart so was not aware of completers. Thanks for your help and great library once again.
from flutter_redux.
Brian,
Since the move to Dart2, I don't seem to be able to use a Completer in the middleware. Is there a way around this or a better way to manager the refresh indicator now?
Thanks
from flutter_redux.
Heya @oli107. Hrm, that's interesting -- I haven't run into that problem with Dart 2 + Completers. Is the Completer just not working, or are you running into type errors of some kind?
from flutter_redux.
Hah, no worries at all :) We've all been there!
from flutter_redux.
Hi there,
Thank for the great library. I ran into this exact problem. I am using flutter redux with dart 2. I also read the example you gave above. But I couldn't make it work.
This is the error message I got:
I/flutter ( 5098): The following assertion was thrown building StreamBuilder<_ViewModel>(dirty, state: I/flutter ( 5098): _StreamBuilderBaseState<_ViewModel, AsyncSnapshot<_ViewModel>>#cc65e): I/flutter ( 5098): type '() => Future<dynamic>' is not a subtype of type '() => Future<Null>'
The weird thing is the program still compiles and application is running, but when I swipe down to refresh. The program stop working as spinner does not disappear and that error message print in the console.
from flutter_redux.
Hey hey, sorry about the delay! Came back to quite a bit of work :)
Here's the best solution I could think of:
1. Create an Action that contains a `Completer` 2. Return the `action.completer.future` to the `onRefresh` function of the `RefreshIndicator` 3. Write a `Middleware` that listens for this action. It handles: * Making an async call to a database or web api * After the async call completes, does the normal thing: dispatches an action with some payload * Also performs `action.completer.complete`. This will inform the RefreshIndicator that the async work is complete, and will hide the spinner.
Example here: https://github.com/brianegan/flutter_redux/blob/refresh-indicator-example/example/lib/main.dart#L10
I'll try to think if there's a good way to make this easier in some way... Maybe a generic action or middleware.
Hey, thanks for the awesome work and continuous support.
For this issue, can we wrap every object in the state inside another object which contains the information about the status, the actual data (Generic) and a message. Something like this:
Resource{
dynamic data,
String status,
dynamic message
}
Making three plausible condition as follows:
-
When there is an API or DB call, we can tell reducer to return this object:
Resource(null, Status.LOADING, "Entity
Loading") -
When there is response from API or DB
Resource(data, Status.SUCCESS, "Entity
Loaded") -
When there is an error in response:
Resource(null, Status.ERROR, "Error in theEntity
")
from flutter_redux.
I've released a new package called declarative_refresh_indicator which takes a boolean refreshing
value and is implicitly animated. This should be easier to connect to Redux state.
from flutter_redux.
Related Issues (20)
- How to not accept change notifications when the Router is not at the top level, and then take the initiative to obtain the changes when the Router returns to the top level HOT 1
- onDidChange called twice HOT 8
- Is this package still actively maintained?
- `onWillChange` and `onDidChange` errors are swallowed HOT 4
- Warning related to `?` operator on Linux/Dev channel. HOT 2
- Import Store data type
- Widgets binding error after upgrading flutter version to 2.13.0-0.1.pre HOT 1
- onDidChange not working as expected HOT 3
- Build warning with Flutter 3.0 HOT 5
- flutter_redux depends on flutter_redux, version solving failed HOT 3
- onWillChange and onDidChange called twice after state update HOT 2
- Passing Store as an argument to MethodChannel and Background Isolate
- Question on how to use the reducer right way HOT 2
- [Question]: Any thoughts on difference between ignoreChange and distinct?
- Socket and Flutter_redux HOT 18
- Dispatching an action that does not alter state rerenders the widget, causing an infinite loop HOT 5
- How to watch redux store change inside useEffect? HOT 5
- [Question] is it expected that the store doesn't run until frames render? HOT 1
- After push a new page, and Dispatching an action, there is an infinite loop
- Is flutter redux actively maintained HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flutter_redux.