gpt4 book ai didi

Flutter Riverpod StateNotifier 在页面构建时加载数据

转载 作者:行者123 更新时间:2023-12-05 00:44:53 24 4
gpt4 key购买 nike

我正在开发一个新的应用程序并使用状态通知器测试 Riverpod,并且有一个关于在构建页面时可以在哪里加载初始数据的问题。

我有以下状态类:

abstract class SalesOrderListState extends Equatable {
const SalesOrderListState();

@override
List<Object> get props => [];
}

class SalesOrderListInitial extends SalesOrderListState {}

class SalesOrderListLoading extends SalesOrderListState {}

class SalesOrderListSuccess extends SalesOrderListState {
final List<SalesOrderListItem> salesOrderListItems;

SalesOrderListSuccess({
@required this.salesOrderListItems,
});

@override
List<Object> get props => [salesOrderListItems];
}

class SalesOrderListError extends SalesOrderListState {
final String error;

const SalesOrderListError({@required this.error});

@override
List<Object> get props => [error];
}

还有这个 state_notifier 类:

final salesOrderListStateNotifierProvider =
StateNotifierProvider<SalesOrderListStateNotifier>(
(ref) => SalesOrderListStateNotifier(ref.read));

class SalesOrderListStateNotifier extends StateNotifier<SalesOrderListState> {
final Reader read;

SalesOrderListStateNotifier(this.read) : super(SalesOrderListInitial());

Future<void> getSalesOrders() async {
final SalesOrderListUseCase _salesOrderListUseCase =
read(salesOrderListUseCaseProvider);

state = SalesOrderListLoading();
final result = await _salesOrderListUseCase.getSalesOrders();
result.fold(
(failure) =>
state = SalesOrderListError(error: mapFailureToMessage(failure)),
(salesOrderListItems) async {
state = SalesOrderListSuccess(salesOrderListItems: salesOrderListItems);
},
);
}
}

现在我的 widged 类(页面)像这样使用状态通知器:

 ...

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(),
),
body: Consumer(
builder: (context, watch, child) {
final state = watch(salesOrderListStateNotifier.state);
switch (state.runtimeType) {
case SalesOrderListSuccess:
final _salesOrders = (state as SalesOrderListSuccess).salesOrderListItems;
...
print sales orders
...
);
break;
case SalesOrderListError:
return MessageDisplayWidget(
message: (state as SalesOrderListError).error,
);
break;
default:
return LoadingWidget();
}
},
),
);
}

但在页面加载之前,我必须调用

context
.read(salesOrderListStateNotifierProvider)
.getSalesOrders()

最好的方法是什么?

我已经尝试过使用 statefull 小部件并像这样覆盖 initstate

void initState() {
super.initState();
Future.delayed(Duration.zero).then(
(_) => context
.read(salesOrderListStateNotifierProvider)
.getSalesOrders(),
);
// WidgetsBinding.instance.addPostFrameCallback(
// (_) => context
// .read(salesOrderListStateNotifierProvider)
// .getSalesOrders(),
// );
}

这可行,但我不确定这是否是最好的方法。

我有很多页面必须在绘制数据之前加载数据,我不确定将所有这些页面转换为有状态的小部件以覆盖 initstate 是否是最佳解决方案。

最佳答案

github上@iamarnas给了我解决方法,在statenotifier构造函数里面调用fetch方法

class UserNotifier extends StateNotifier<User>{
UserNotifier() : super(User()){
fetchData(); // It's same as initState();
}
}

关于Flutter Riverpod StateNotifier 在页面构建时加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65305993/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com