【问题标题】:Tried to call Provider.of<dynamic>. This is likely a mistake and is therefore unsupported试图调用 Provider.of<dynamic>。这可能是一个错误,因此不受支持
【发布时间】:2021-07-07 20:30:59
【问题描述】:

尝试获取当前用户的 uid 并向其发送文本,但面临 Provider 实现的错误。

尝试调用 Provider.of。这很可能是一个错误,并且是 因此不受支持。如果你想公开一个变量 任何事情,请考虑将dynamic 改为Object。 'package:provider/src/provider.dart':断言失败:第 307 行 pos 7: 'T != 动态'

这是使用提供程序的代码

 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();
                }
              })
        ],
      ),
    );

获取当前uid的函数

//GET UID
  Future<String> getCurrentUid() async {
    return _auth.currentUser.uid;
  }

我该如何解决这个问题? 非常感谢任何帮助! 提前谢谢你。

【问题讨论】:

    标签: firebase flutter flutter-provider


    【解决方案1】:

    只需替换

    future: Provider.of(context, listen: false).auth.getCurrentUid(),
    

    future: Provider.of<YOUR_PROVIDER_CLASSNAME>(context, listen: false).auth.getCurrentUid(),
    

    你应该没问题。

    您可以在不指定应提供的类的情况下使用Provider.of 的唯一时刻是初始化提供程序的变量时。

    MyProvider _myProvider = Provider.of(context, listen: false);
    

    【讨论】:

      【解决方案2】:

      只需告诉提供者它是哪种类型的“提供者”类。

      future: Provider.of<CLASSNAME>(context, listen: false).auth.getCurrentUid(),
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-06-29
        • 1970-01-01
        • 1970-01-01
        • 2019-12-27
        • 2018-08-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多