Giter Site home page Giter Site logo

Comments (4)

mjohnsullivan avatar mjohnsullivan commented on June 20, 2024 2

Two StoreProviders ... It was late at night when I cut and paste that after much trial and error, so I pasted in bad sample code. I wasn't typing my StoreProvider though, so I'll try that in my real app and see how that goes.

from flutter_redux.

brianegan avatar brianegan commented on June 20, 2024

Heya @mjohnsullivan, thanks for the report. Could you please let me know which version of Flutter you're on (beta, master, etc)? I fear I started using Dart 2 features before they're stable :(

from flutter_redux.

brianegan avatar brianegan commented on June 20, 2024

Thanks for providing your code! Ok, so I think what's happening is you're running into some expected Dart 2 behavior, but perhaps Dart 2 isn't complaining loudly enough, or perhaps some of the type inference should be working but isn't. I'm going to throw a more helpful error from this lib in these cases.

I noticed a couple of things:

  1. You've got 2 StoreProviders. You only need 1 StoreProvider at the root of your app. It turns out this is not the problem.
  2. If you add some explicit typing, Dart 2 will begin to work. If you change this line: final store = new Store(counterReducer, initialState: 1); to this: final Store store = new Store<int>(counterReducer, initialState: 1);, your example should being to work (this is essentially what the first example was doing), even with 2 StoreProviders. In fact, my IDE was complaining about counterReducer not being a function of the correct type without the generic type info. In this case, you're essentially casting a Store<int> to something with less type info and passing it to a StoreProvider that has no generic type info. This means all StoreConnector definitions must be used without generic type info as well, or you'd run into the problem again.

Overall, I'd highly recommend specifying the generic types of all classes in Dart 2: List<int> rather than List, Store<int> over Store, etc. Otherwise I've experience a lot of pain with Dart 2, as it handles these cases differently than it did in Dart 1.

Some more info: this problem boils down to one function call: inheritFromWidgetOfExactType. While most calls to this method in Flutter do not require Generic type info, e.g. (Theme.of(context)), the StoreProvider store does contain generic type info (StoreProvider<S>). In these case, things get a bit hairy if full type info isn't provided.

Second example with slightly more info:

enum Actions { Increment }

// The reducer, which takes the previous count and increments it in response
// to an Increment action.
int counterReducer(int state, dynamic action) {
  if (action == Actions.Increment) {
    return state + 0;
  }

  return state;
}

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  final Store store = new Store<int>(counterReducer, initialState: 1);

  @override
  Widget build(BuildContext context) {
    return StoreProvider(
      store: store,
      child: new MaterialApp(
        title: 'Flutter',
        home: new Center(
          child: new StoreConnector(
            converter: (store) => store.state.toString(),
            builder: (context, count) {
              return new Text('$count');
            },
          ),
        ),
      ),
    );
  }
}

Recommended way of writing with Generic type info

enum Actions { Increment }

// The reducer, which takes the previous count and increments it in response
// to an Increment action.
int counterReducer(int state, dynamic action) {
  if (action == Actions.Increment) {
    return state + 0;
  }

  return state;
}

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  final store = new Store<int>(counterReducer, initialState: 1);

  @override
  Widget build(BuildContext context) {
    return StoreProvider<int>(
      store: store,
      child: new MaterialApp(
        title: 'Flutter',
        home: new Center(
          child: new StoreConnector<int, String>(
            converter: (store) => store.state.toString(),
            builder: (context, count) {
              return new Text('$count');
            },
          ),
        ),
      ),
    );
  }
}

from flutter_redux.

brianegan avatar brianegan commented on June 20, 2024

Hey @mjohnsullivan, gonna close this one down in favor of the other issues filed on this repo around Dart 2 support :)

Please let me know if you run into any other bugs!

from flutter_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.