Comments (4)
I've modified the repository to include the init function and it works fine for me. I think this simplify a lot the usage of the library.
The usage would be something like that:
/// Note that you must add AppBuilder too.
class AppScreen extends StoreConnector<App, AppBuilder, AppActions, App> {
@override
App connect(App state) => state;
@override
void init(BuildContext context, Store<App, AppBuilder, AppActions> store) {
// You can use the store to bind listeners and context to interact programmatically with the widget.
// Examples: take focus out of input, scroll the listview, show a snackbar, etc...
store.actionStream(AppActionsName.addTodo).listen((change) {});
}
@override
Widget build(BuildContext context, App state, AppActions actions) {
return Scaffold(
body: Center(
child: Text('Hello World'),
),
);
}
}
To make types work I had to add AppBuilder
to StoreConnector
,
@davidmarne Can you please take a look and tell me what you think.
Fork: https://github.com/ga6559/flutter_built_redux
from flutter_built_redux.
When I wrote the lib I was against subscribing to anything other than state changes, but I hadn't considered this use case.
I'm guessing dataCorrupted is dispatched by a middleware when an http request or some other async side effect fails? If not I would be curious to get a little more context about your use case.
If that is the case you could consider doing something like this where you send the context as part of the action payload that initiated the http request that caused the error:
Action Payload
class FormSubmitPayload {
final ScaffoldState scaffold;
// other fields
}
Widget
build(context) =>
MyFormWidget(
onSubmit: () {
actions.onFormSubmit(new FormSubmitPayload(
Scaffold.of(context),
..., // other fields,
));
}
);
Middleware Handler
void MiddlewareApi<Counter, CounterBuilder, CounterActions> api, ActionHandler next, Action<FormSubmitPayload> action
http.get(url)
.then(() {
action.payload.scaffold.showSnackBar(SnackBar(content: Text('Successful Transaction')));
api.actions.success();
})
.catchError(() {
action.payload.scaffold.showSnackBar(SnackBar(content: Text('Data Corrupted')));
api.actions.error();
});
}
Regardless, if something like this doesn't work for you I would like to better understand why. I'm not against adding the init function but I think that the problem could be solved in another way.
from flutter_built_redux.
@davidmarne Thanks for the response.
You got it right, the dataCorrupted is called from a middleware that fails to load the data. Unfortunately your solution didn't work for me because I am sharing code between flutter and web, so I can't call showSnackBar
from a middleware.
I ended up creating a lastEvent
property in my state that I can listen to inside my views and react to it programmatically. In my views I do something like this if (state.lastEvent == AppActionNames.dataCorrupted.name)
. I'd prefer to use the built_redux built-in actionStream
, but for my use case this works just fine.
I still don't see how we could use the stream
built_redux gives us inside a flutter component. I was thinking that maybe if we could override initState
and dispose
inside a StoreConnector, we could bind the listener inside initState and close it in dispose. This is what we do with flutter core components like the FocusNode
.
Example:
@override
void initState() {
super.initState();
widget.focusNode.addListener(_ensureVisible);
}
@override
void dispose() {
super.dispose();
widget.focusNode.removeListener(_ensureVisible);
}
from flutter_built_redux.
Also, ReduxProvider
could also give us an of
method like this. That way we could access the store and its stream anywhere in flutter using ReduxProvider.of(context).store
.
from flutter_built_redux.
Related Issues (20)
- Update built_value to >= 4 <= 6 ? HOT 2
- AnalysisException: Cannot compute LIBRARY_ELEMENT for Instance of 'AssetBasedSource' HOT 3
- Cannot resolve class 'StreamTransformerBase' from 'MimeMultipartTransformer' HOT 4
- Don't use the WidgetBuilder name HOT 6
- How to: store.actionStream example HOT 1
- 'flutter packages get' always not responding. HOT 3
- Question: Is the example up-to-date? HOT 1
- Nested State does not update the UI HOT 2
- Need update to flutter engine 2.0 HOT 2
- Example of using the Navigator HOT 1
- Question: dispatching action on widget initialization HOT 4
- Question: Best approach for widget testing
- support for flutter sdk 2.1.0.dev
- support for flutter sdk 2.1.0.dev
- Unsubscribe to state when not active route? HOT 1
- support for flutter sdk 2.1.0.dev
- Incompatible with built_redux 8.0.0-nullsafety.0
- Cannot build app after migrating to Flutter 2.0.0 HOT 1
- support for flutter sdk 2.1.0.dev
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_built_redux.