flutter_built_redux icon indicating copy to clipboard operation
flutter_built_redux copied to clipboard

upgrade: Flutter 2

Open maxlapides opened this issue 3 years ago • 3 comments

  • Upgrade to NNBD
  • Replace deprecated method BuildContext.inheritFromWidgetOfExactType
  • Replace deprecated RaisedButton with ElevatedButton in example

maxlapides avatar Feb 05 '21 00:02 maxlapides

@maxlapides What's the status on this? Is it working for your use case / dependencies? Running into a blocker without this PR or something like it merged. Would be really neat to support it

bZichett avatar Jan 26 '22 21:01 bZichett

@bZichett It seems like this flutter_built_redux project isn't under maintenance anymore, so we migrated away. We're still using built_redux though, so we implemented our own Flutter bindings. I'm happy to share this code with you since it's quite simple. We're using Provider to inject the Redux store dependency, and then we wrote some custom Dart extensions to read/watch values from Redux:

extension ReduxProviderExtension on BuildContext {
  T watchRedux<T>(T Function(AppState state) selector) {
    return select<ReduxNotifier, T>((notifier) => selector(notifier.state));
  }

  T readRedux<T>(T Function(AppState state) selector) {
    return selector(read<ReduxNotifier>().state);
  }
}

class ReduxNotifier extends ChangeNotifier {
  ReduxNotifier(this.store) {
    _nextStateSubscription = store.nextState.listen((_) => notifyListeners());
  }

  final AppStore store;

  AppState get state => store.state;

  late StreamSubscription _nextStateSubscription;

  @override
  void dispose() {
    _nextStateSubscription.cancel();
    super.dispose();
  }
}

An instance of ReduxNotifier needs to be provided at the root level of your app:

ChangeNotifierProvider<ReduxNotifier>(
  create: (_) => ReduxNotifier(store),
)

And then you can read or watch values from Redux anywhere in your app!

final userId = context.watchRedux((state) => state.userId);

maxlapides avatar Jan 26 '22 22:01 maxlapides

@maxlapides That is most appreciated. I will give it a try tomorrow :+1:

bZichett avatar Jan 27 '22 02:01 bZichett