简体   繁体   English

试图调用 Provider.of<dynamic> . 这可能是一个错误,因此不受支持</dynamic>

[英]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 to Object 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.

相关问题 如何使用 class Provider.of 获得 Futur function 调用的返回值? - How to get the return of Futur function call with class Provider.of? NoSuchMethodError: 试图调用一个非函数,如 Flutter - NoSuchMethodError: tried to call a non-function, such as null: 'dart.global.firebase.messaging' in Flutter 不支持的操作:Web不支持toImage - Unsupported operation: toImage is not supported on the Web GCP Cloud Run:应用程序执行可能失败 - GCP Cloud Run : Application exec likely failed AngularFire - 新的提供者语法 - AngularFire - New Provider Syntax FirebaseException ([cloud_firestore/unavailable] 该服务当前不可用。这很可能是暂时性情况,可能会更正 - FirebaseException ([cloud_firestore/unavailable] The service is currently unavailable. This is a most likely a transient condition and may be correcte 如何使用动态时间延迟调用 lambda function - How can I delay call lambda function with dynamic time 如何从我的步骤函数工作流中调用动态活动? - How to call a dynamic activity from my step functions workflow? AWS ECS 集群容量提供程序 - AWS ECS cluster Capacity Provider 架构不支持的关系,有很多(UUID,没有 Gorm 模型) - Unsupported relations for schema, Has Many (UUID, Without Gorm Model)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM