Error in provider flutter context listen:false

huangapple go评论58阅读模式
英文:

Error in provider flutter context listen:false

问题

因此,我正在使用Flutter和Dart制作一个OTP登录的应用程序,当我提供电话号码时,它会显示一个错误。

代码如下:

void sendPhoneNumber() {
    final ap = Provider.of<AuthProvider>(context, listen: false);
    String phoneNumber = phoneController.text.trim();
    ap.signInWithPhone(context, "+${selectedCountry.phoneCode}$phoneNumber");
}

错误出现在这里:"Tried to listen to a value exposed with provider, from outside of the widget tree."

有什么想法可以解决吗?

英文:

So, I'm making an app of OTP login with flutter and dart and when I provide the pohone number it's shows an error

The code is this:

 void sendPhoneNumber() {
    final ap = Provider.of&lt;AuthProvider&gt;(context, listen: false);
    String phoneNumber = phoneController.text.trim();
    ap.signInWithPhone(context, &quot;+{selectedCountry.phoneCode}$phoneNumber&quot;);
  }

The error it's when I'm in final ap, there shows this: "Tried to listen to a value exposed with provider, from outside of the widget tree."

Any idea what can i do to solve it?

答案1

得分: 0

似乎你忘记创建一个对象

单一

Provider(
  create: (_) => MyModel(),
  child: ...
)

多个

runApp(
  MultiProvider(
    providers: [
      ChangeNotifierProvider(create: (context) => myViewModel()),
    ]
  )
)
英文:

Seem like you're forgetting to create an object

Single

Provider(
  create: (_) =&gt; MyModel(),
  child: ...
)

Multi

    runApp(
            MultiProvider(
              providers: [
                ChangeNotifierProvider(create: (context) =&gt; myViewModel()),
                       ]
                      )

huangapple
  • 本文由 发表于 2023年2月19日 09:19:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497428.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定