Giter VIP home page Giter VIP logo

Comments (4)

ga6559 avatar ga6559 commented on June 1, 2024

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.

davidmarne avatar davidmarne commented on June 1, 2024

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.

ga6559 avatar ga6559 commented on June 1, 2024

@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.

ga6559 avatar ga6559 commented on June 1, 2024

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)

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.