Flutter Riverpod FutureProvider一次又一次地不断开发
我正在使用Riverpod的FutureProvider与家人一起使用。FutureProvider一次又一次地运行。它仅显示加载对话框。此外,热重新加载停止工作。FutureProvider在没有家人的情况下运行良好。请帮忙找出问题所在Flutter Riverpod FutureProvider一次又一次地不断开发,flutter,dart,flutter-provider,riverpod,Flutter,Dart,Flutter Provider,Riverpod,我正在使用Riverpod的FutureProvider与家人一起使用。FutureProvider一次又一次地运行。它仅显示加载对话框。此外,热重新加载停止工作。FutureProvider在没有家人的情况下运行良好。请帮忙找出问题所在 最终星历提供程序= 提供者((参考)=>ApiService(“https://localhost")); 最终星历FutureProvider=FutureProvider.family .autoDispose((参考,数据)异步{ var响应=等待参考
最终星历提供程序=
提供者((参考)=>ApiService(“https://localhost"));
最终星历FutureProvider=FutureProvider.family
.autoDispose((参考,数据)异步{
var响应=等待参考读取(星历提供程序).getData(数据);
打印(EpheModel.fromJSON(响应));
返回EpheModel.fromJSON(响应);
});
类Kundlis扩展了ConsumerWidget{
静态常量routeName=“/kundlis”;
@凌驾
小部件构建(BuildContext上下文,ScopeDrawer监视){
final AsyncValue kundlis=watch(星历未来提供者({}));
返回脚手架(
appBar:appBar(
标题:文本(“Kundlis”),
),
抽屉:AppDrawer(),
身体:昆迪斯。什么时候(
数据:(kundli)=>Center(子项:Text(kundli.toString()),
正在加载:()=>ProgressDialog(消息:“正在获取详细信息…”),
错误:(消息,st)=>
buildErrorSnackbar(上下文,$message'));
}
}
班级服务{
最终字符串url;
ApiService(this.url);
未来getData(映射数据)异步{
试一试{
http.Response-Response=wait http.post(url+“/ephe”,
标题:{'Content-Type':'application/json'},
正文:JSONECODE(数据));
如果(response.statusCode==200){
返回数据;
}否则{
抛出异常(“获取详细信息时出错”);
}
}论SocketException{
抛出异常(“没有互联网连接”);
}关于HttpException{
抛出异常(“获取详细信息时出错”);
}
}
}
{}={}
。由于.family
,您每次调用watch(星历未来提供者({}))
时都会创建一个全新的提供者。若要通过族选择以前生成的提供程序,必须传递相同的值。而且{}永远不会与{}相同,保证。:) {}={}
。由于.family
,您每次调用watch(星历未来提供者({}))
时都会创建一个全新的提供者。若要通过族选择以前生成的提供程序,必须传递相同的值。而且{}永远不会与{}相同,保证。:) 注意:不要在提供程序中使用ref.read
。首选ref.watch
@alexhartford我用的是ref.watch。同样的错误也在发生。在看了YouTube上的ResoCoder教程后,刚将手表改为read。注意:不要在提供者内部使用ref.read
。首选ref.watch
@alexhartford我用的是ref.watch。同样的错误也在发生。在看了YouTube上的ResoCoder教程后,刚将手表改为read。也没用,谢谢。情况就是这样。但那个么我怎样才能在族中传递映射值呢?它必须是常量。你刚才说过constempty={}每次使用和空
时,它们都是相同的。谢谢。情况就是这样。但那个么我怎样才能在族中传递映射值呢?它必须是常量。你刚才说过constempty={}每次使用和空
时,它们都是相同的。
final ephemerisProvider =
Provider((ref) => ApiService("https://localhost"));
final ephemerisFutureProvider = FutureProvider.family
.autoDispose<EpheModel, Map<String, dynamic>>((ref, data) async {
var response = await ref.read(ephemerisProvider).getData(data);
print(EpheModel.fromJSON(response));
return EpheModel.fromJSON(response);
});
class Kundlis extends ConsumerWidget {
static const routeName = "/kundlis";
@override
Widget build(BuildContext context, ScopedReader watch) {
final AsyncValue<EpheModel> kundlis = watch(ephemerisFutureProvider({}));
return Scaffold(
appBar: AppBar(
title: Text("Kundlis"),
),
drawer: AppDrawer(),
body: kundlis.when(
data: (kundli) => Center(child: Text(kundli.toString())),
loading: () => ProgressDialog(message: "Fetching Details..."),
error: (message, st) =>
CustomSnackBar.buildErrorSnackbar(context, '$message')));
}
}
class ApiService {
final String url;
ApiService(this.url);
Future<Map<String, dynamic>> getData(Map<String, dynamic> data) async {
try {
http.Response response = await http.post(url + "/ephe",
headers: <String, String>{'Content-Type': 'application/json'},
body: jsonEncode(data));
if (response.statusCode == 200) {
return data;
} else {
throw Exception("Error Fetching Details");
}
} on SocketException {
throw Exception("No Internet Connection");
} on HttpException {
throw Exception("Error Fetching Details");
}
}
}