【发布时间】:2021-08-17 18:52:14
【问题描述】:
在我的这个简单项目中,我使用HookWidget 和Riverpod 实现,这里我尝试在每个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));
}
【问题讨论】: