rrousselGit / state_notifier

ValueNotifier, but outside Flutter and with some extra perks
MIT License
311 stars 28 forks source link

Document that flutte_state_notifier's LocatorMixin is unsupported #62

Open tbm98 opened 4 years ago

tbm98 commented 4 years ago

Describe the bug StateNotifier not call initState.

To Reproduce

my class:

final homeStateProvider = StateNotifierProvider((ref) => HomeStateNotifier());

class HomeStateNotifier extends StateNotifier<HomeState> with LocatorMixin {
  HomeStateNotifier() : super(const HomeState());

  @override
  void initState() {
    super.initState();
    logs('HomeStateNotifier init state');
    _loadTrending();
    _loadCategory();
    _loadPromotion();
    _loadNewServices();
  }

usage

class TrendingVendor extends HookWidget {
  @override
  Widget build(BuildContext context) {
    final result = useProvider(
        homeStateProvider.state.select((value) => value?.trendingResult));
    if (result == null) {
      return const CircleLoading();
    }
    if (result.isFail) {
      return FailWidget(result.message);
    }
rrousselGit commented 4 years ago

LocatorMixin is not supported by Riverpod.

tbm98 commented 4 years ago

Yep. just call in constructor and it worked. I think it should be noted in the documentation.

befora commented 3 years ago

I was looking for LocatorMixin support but found ProviderReference instead. You can use ProviderReference to read your other services.

Providers:

final myLocalServiceProvider = Provider((ref) => MyLocalService());

final myChangeNotifierProvider = ChangeNotifierProvider((ref) => MyChangeNotifier(ref));

ChangeNotifier:

class MyChangeNotifier extends ChangeNotifier {
  MyChangeNotifier(this.ref);

  final ProviderReference ref;

  int count = 43;

  void add() {
    count++;
    notifyListeners();
    ref.read(myLocalServiceProvider ).saveLocal(count);
  }

  void subtract() {
    count--;
    notifyListeners();
    ref.read(myLocalServiceProvider ).saveLocal(count);
  }
}
rrousselGit commented 2 years ago

I don't think this is the responsibility of Riverpod to mention this, as LocatorMixin is not exported by Riverpod.

I'll move this to state_notifier instead