【问题标题】:Flutter Riverpod using ProviderListener to show SnakeBarFlutter Riverpod 使用 ProviderListener 展示 SnakeBar
【发布时间】:2021-08-17 18:52:14
【问题描述】:

在我的这个简单项目中,我使用HookWidgetRiverpod 实现,这里我尝试在每个Riverpod 的useProvider 上显示一个简单的SnakeBar,但我得到错误,我做不到,我的提供者回调是小部件,我不确定如何显示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) {},
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

我认为我应该使用ProviderListener,但我不确定如何实现它

我的供应商:

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));

}

【问题讨论】:

    标签: flutter riverpod


    【解决方案1】:

    我找到了如何解决这个问题。解决方案是用ProviderListener 包裹Scaffold

    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(
    

    【讨论】:

      【解决方案2】:

      您好,我不知道这是否可以帮助您,但我曾经自己编写一个包来显示来自通知程序的错误,现在我发布到 pub.dev

      https://pub.dev/packages/riverpod_messages/versions/1.0.0

      告诉我!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-05-03
        • 1970-01-01
        • 1970-01-01
        • 2022-07-17
        • 2021-04-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多