英文:
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<AuthProvider>(context, listen: false);
String phoneNumber = phoneController.text.trim();
ap.signInWithPhone(context, "+{selectedCountry.phoneCode}$phoneNumber");
}
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: (_) => MyModel(),
child: ...
)
Multi
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => myViewModel()),
]
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论