Flutter Riverpod using ProviderListener to show SnakeBar

Viewed 610

in this simple of my project i used HookWidget with Riverpod implementation, here i try to show a simple SnakeBar on each Riverpod's useProvider but i get error and i can't do it, my providers callbacks are widgets and i'm not sure how can i show a SnakeBar

class Profile extends HookWidget {
  @override
  Widget build(BuildContext context) {
    final nameFamily = useTextEditingController();

    return Container(
      color: DefaultColors.$darkBlue,
      child: SafeArea(
        child: Directionality(
          textDirection: TextDirection.rtl,
          child: Scaffold(
            body: ListView(
              children: [
                Form(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      useProvider(profileProvider).when(
                        idle: () {}
                        loading: () {},
                        success: (value) {},
                        error: (error, stackTrace) {},
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

i think i should use ProviderListener but i'm not sure how can implement that

my providers:

final updateProfileProvider = Provider((ref) => UpdateProfileRepository(ref.read));
final profileProvider = StateNotifierProvider<UpdateProfileNotifier, NetworkRequestState<UpdateProfileStructure>>(
        (ref) => UpdateProfileNotifier(ref.watch(updateProfileProvider)));


class UpdateProfileRepository {
  final Reader _reader;

  UpdateProfileRepository(this._reader);

  Future<UpdateProfileStructure> requestUpdateProfile(String nameFamily, String apiToken) async {
    try {
      //...

      return UpdateProfileStructure.fromJson(response.data as Map<String, dynamic>);
    } on DioError catch (e) {
      throw e.error as Object;
    }
  }
}

class UpdateProfileNotifier extends RequestStateNotifier<UpdateProfileStructure> {
  final UpdateProfileRepository _updateProfileRepository;

  UpdateProfileNotifier(this._updateProfileRepository);

  Future<NetworkRequestState<UpdateProfileStructure>> updateNameFamily(String nameFamily, String apiToken) =>
      makeRequest(() => _updateProfileRepository.requestUpdateProfile(nameFamily,apiToken));

}
2 Answers

I found how can i resolve this issue. solution is wraping Scaffold with ProviderListener:

child: ProviderListener<NetworkRequestState<DiscountCodeStructure>>(
  provider: discountProvider,
  onChange: (BuildContext context, NetworkRequestState<DiscountCodeStructure> discountListener) {
    discountListener.when(
        idle: () {},
        loading: () {},
        success: (value) {
          Functions.i.showSnackBar(
              context: context, message: _res.message, backgroundColor: _b, color: Colors.white);
        },
        error: (error, stackTrace) {
          Functions.i.showSnackBar(
              context: context, message: error, backgroundColor: _b, color: Colors.white);
        });
  },
  child: Scaffold(
Related