Riverpod futureProvider.FAMILY返回NULL
我有一个带有家庭参数的Future -provider,如下所示:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
class I_Rem_Params {
final String adminID;
final String companyID;
final int month;
I_Rem_Params(
{required this.adminID, required this.companyID, required this.month});
}
final iRemReportsFamily =
FutureProvider.family<double, I_Rem_Params>((ref, iremparams) async {
final db = FirebaseFirestore.instance;
final admins = db.collection("Admins");
final admin = admins.doc(iremparams.adminID);
final companies = admin.collection("Companies");
final company = companies.doc(iremparams.companyID);
final imports = company.collection("Imports");
final getImports = await imports.get();
final val = getImports.docs.length.toDouble();
print("value at provider : " + val.toString());
return val;
});
我正在打印FutureProvider在下面发送的值之前:
value at provider : 2
我在widget sine处得到和打印异步的Asyncvalue:
AsyncValue<double> juneTotalValue = ref.watch(iRemReportsFamily(
I_Rem_Params(
adminID: adminID!, companyID: widget.companyID, month: 5)));
print("value at widget : " + juneTotalValue.value.toString());
print("runtimetype at widget : " + juneTotalValue.runtimeType.toString());
:如下:如下:如下:
value at widget : null
runtimetype at widget : AsyncLoading<double>
Asyncvalue在widget sine上按照下面的方式打印出来:并注入Asyncvalue,如下所示
....... children: [
juneTotalValue.when(
data: (data) => Text(data.toString()),
error: (error, stack) => Text(error.toString()),
loading: () => const CircularProgressIndicator()),
const SizedBox(
height: 38,
),........
如下:如下:如下:处理异步的处理不显示未来推销员发送的2的值,而仅显示圆形ProgressIndicator()的加载窗口小部件。
问题:尽管Future -Provider发送了一个数字,但为什么异步是无效的?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这主要与您如何创建FutureProvider有关。
因为。家庭修饰符为每个独特价值创建一个新的提供商,您需要确认输入值的 hashcode 每次调用提供商时都是相同的。如果不是这样,每次都会创建一个新的提供商。如果您正在使用ref.watch,则可以陷入循环中:
也可以在参数限制 RiverPod文档的部分。通常,您应该使用原始数据类型(例如int,bool,double或string)创建。家庭提供商。
如果您需要使用其他东西,那么您需要使用诸如。
This mainly has to do with how you are creating your FutureProvider.family.
Since the .family modifier creates a new provider for each unique value that is passed to it, you need to confirm that the hashcode of the input value is the same each time you call the provider. If it's not, a new provider will be created each time. If you're using ref.watch, you can get caught in a loop:
This is also pointed out in the Parameter restrictions section of the Riverpod docs. In general, you should create your .family providers using a primitive data type (like int, bool, double, or string).
If you need to use something else, then you'll need to override the hashcode method of your object using a package like equatable.
Randal的解决方案解决了问题。
Randal's solution has solved the problem.