Skip to content

Why we need to pass the String type in StoreConnector ? #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
fadhilkemal opened this issue Aug 1, 2018 · 1 comment
Closed

Why we need to pass the String type in StoreConnector ? #67

fadhilkemal opened this issue Aug 1, 2018 · 1 comment

Comments

@fadhilkemal
Copy link

I found this on the example

new StoreConnector<int, String>(

new StoreConnector<int, String>(
                  converter: (store) => store.state.toString(),
                  builder: (context, count) {
                    return new Text(
                      count,
                      style: Theme.of(context).textTheme.display1,
                    );
                  },
                )

1. Is there any performance Issue if I change the second Type Parameter from String to Int ?

new StoreConnector<int, int>(
                  converter: (store) => store.state,
                  builder: (context, state) {
                    return new Text(
                      "${state}",
                      style: Theme.of(context).textTheme.display1,
                    );
                  },
                )

2. Because I have another flutter-redux example that looks like this


StoreConnector<AppState, int>(
                converter: (store) => store.state.itemCount,
                builder: (context, count) {
                  return Text(
                    "$count",
                    style: Theme.of(context).textTheme.display1,
                  );
                },
              ),


then I changed it to this

StoreConnector<AppState, AppState>(
                converter: (store) => store.state,
                builder: (context, state) {
                  return Text(
                    "${state.itemCount}",
                    style: Theme.of(context).textTheme.display1,
                  );
                },
              ),

@brianegan
Copy link
Owner

brianegan commented Aug 1, 2018

The second type parameter provides Type information about what kind of "ViewModel" you're extracting from the Store and passing to the builder function. If you didn't provide the second type, it would have to be dynamic or Object, which means you wouldn't have the proper type information in the builder function, nor would you have any type checking to ensure the converter function returns a value of the Type you expect.

If you want to pass through just the state of the Store, it's not the approach I would take, but there's nothing stopping you from doing it.

Generally I use the converter to gather all the data I need for the builder function, so I don't need to do any work inside the builder function other than create a Widget with the values I've extracted inside the converter.

The examples above are really simple, and the extra type parameter might not seem necessary, but when you start extracting more values for the builder function, the second Type parameter becomes really important. You can see more complex examples here: https://github.com/brianegan/flutter_architecture_samples/blob/master/example/redux/lib/containers/filter_selector.dart

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants