Home > front end >  Flutter Riverpod using ProviderListener in HookWidget
Flutter Riverpod using ProviderListener in HookWidget

Time:08-12

In previous versions of Riverpod package for Flutter i could use ProviderListener simply into HookWidget and now it's depricated for new versions and i can't found any documentation about that to know how can i use that again

ProviderListener<NetworkRequestState<LoginResponseStructure>>(
provider: loginProvider,
onChange: (BuildContext context,
    NetworkRequestState<LoginResponseStructure> loginListener) {
  loginListener.when(idle: () {
    enableTextField.value = true;
  }, loading: () {
    ...
  }, success: (value) {
    final LoginResponseStructure _res = value as LoginResponseStructure;
    if (_res.statusCode == 1) {
      ...
    } else {
      ...
    }
  }, error: (error, stackTrace) {
    enableTextField.value = true;
  });
},

deprecated error message:

'ProviderListener' is deprecated and shouldn't be used. Use WidgetRef.listen instead.

for example:

class SignUP extends HookWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: DefaultColors.$lightBlue,
      child: SafeArea(
        child: Directionality(
          textDirection: TextDirection.rtl,
          child:
          ProviderListener<NetworkRequestState<LoginResponseStructure>>(
            provider: loginProvider,
            onChange: (BuildContext context,
                NetworkRequestState<LoginResponseStructure> loginListener) {
              loginListener.when(idle: () {
                enableTextField.value = true;
              }, loading: () {
              }, success: (value) {
              }, error: (error, stackTrace) {
              });
            },
            child: Scaffold(
                ),
          ),
        ),
      ),
    );
  }
}

CodePudding user response:

It's mentioned in the error message. You could simply use ref.listen as a hook. Refer to the docs here.

Example:

Note: Haven't tested but this is what your example will look like


class SignUp extends HookConsumerWidget {
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    ref.listen<NetworkRequestState<LoginResponseStructure>>(
      loginProvider, (NetworkRequestState? previousState, NetworkRequestState newState) {
        loginListener.when...;
     });

    return Container(
      color: DefaultColors.$lightBlue,
      child: SafeArea(
        child: Directionality(
          textDirection: TextDirection.rtl,
          child: ...
        ),
      ),
    );
  }
}

  • Related