[英]Tried to call Provider.of<dynamic>. This is likely a mistake and is therefore unsupported
Trying to get and text current user's uid but facing the error with Provider implementation.尝试获取当前用户的 uid 并向其发送文本,但遇到 Provider 实现错误。
Tried to call Provider.of.
试图调用 Provider.of。 This is likely a mistake and is therefore unsupported.
这可能是一个错误,因此不受支持。 If you want to expose a variable that can be anything, consider changing
dynamic
toObject
instead.如果您想公开一个可以是任何内容的变量,请考虑将
dynamic
更改为Object
。 'package:provider/src/provider.dart': Failed assertion: line 307 pos 7: 'T != dynamic''package:provider/src/provider.dart': 断言失败:第 307 行 pos 7: 'T != dynamic'
Here's the code where provider is used这是使用提供者的代码
return SingleChildScrollView(
child: Column(
children: [
FutureBuilder(
future: Provider.of(context, listen: false).auth.getCurrentUid(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return Text("${snapshot.data}");
} else {
return CircularProgressIndicator();
}
})
],
),
);
function to get current uid function 获取当前uid
//GET UID
Future<String> getCurrentUid() async {
return _auth.currentUser.uid;
}
How can i fix this?我怎样才能解决这个问题? Really appreciate any help.
非常感谢任何帮助。 Thank you in advance.
先感谢您。
Just replace只需更换
future: Provider.of(context, listen: false).auth.getCurrentUid(),
with和
future: Provider.of<YOUR_PROVIDER_CLASSNAME>(context, listen: false).auth.getCurrentUid(),
and your should be fine.你应该没问题。
The only moment you can use Provider.of
without specifying the Class that should be provided, is when you're initializing a variable of your provider.您唯一可以使用
Provider.of
而不指定应提供的 Class 的时刻是在初始化提供程序的变量时。
MyProvider _myProvider = Provider.of(context, listen: false);
Just tell the provider which type of "Provider" class is it.只要告诉提供者是哪种类型的“提供者” class 即可。
future: Provider.of<CLASSNAME>(context, listen: false).auth.getCurrentUid(),
I had same issue, the fixed by adding the (Provider Class/ change notifire class) to the provider list which I forgot:).我遇到了同样的问题,通过将(提供者类/更改通知类)添加到我忘记的提供者列表中来解决:)。 In my case, my provider class was "Counter".
就我而言,我的提供商 class 是“柜台”。
void main() {
runApp(
///mocking the providers
MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => Counter()),
],
child: const MyApp(),
),
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.