提供商未将值可能的字符串更新为双倍问题
我对 Flutter 很陌生,并尝试在课程之外自己编写我的第一个应用程序。我正在使用 Flutter Provider 包,并且我能够成功地使用 Provide 读取值,但在写入值以使用 Provider 更新它们时遇到问题。
代码演练
在我的 main.dar 中,我正在调用类上的创建命令 Week()
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => WeekList()),
ChangeNotifierProvider(create: (context) => Week()),
],
...
我认为我的问题可能是在 String< 之间来回切换/code> 和
double
类型。
在我调用的警报 Diolog 中,
String curentbudget = Provider.of<Week>(context).budget.toString();
该部分工作完美,然后我可以将值分配给 TextEditingController
TextEditingController currentBudgetController = TextEditingController(text: curentbudget);
在警报对话框中编辑值后,然后我尝试使用更新功能保存该值:
onPressed: () {
//save new value
double budgetDouble =double.parse(currentBudgetController.text);
Provider.of<Week>(context, listen: false).updateBudget(newBudget: budgetDouble);
//close Dialog
Navigator.of(context).pop();
}
再次,不确定是否不是保存,因为这是一个 double
/ string
问题。
我的周类更新函数如下所示:
//updateBudget
void updateBudget({
required double newBudget,
}) {
Week().budget = newBudget;
notifyListeners();
}
我尝试更新预算的位置是:
child: Text(
Provider.of<Week>(context).budget.toString(),
style: const TextStyle(fontSize: 25.0),
),
故障排除:
我使用调试器单步执行,当我将值更新为 300.0 时,我成功得到了该值通过更新函数获取值:
Week().budget = newBudget;
下一行是 notifyListeners();
,由于某种原因,看起来这并没有更新我视图中的值。
I am new to flutter and attempting to write my first app on my own outside of a course. I am working with using the Flutter Provider package, and I am successfully able to read values using Provide, but I am having issues writing over values to update them with Provider.
Code Walk Through
In my main.dar I am calling the create command on my class Week()
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => WeekList()),
ChangeNotifierProvider(create: (context) => Week()),
],
...
I think my issue might be going back and forth between String
and double
types.
In an alert Diolog I am calling,
String curentbudget = Provider.of<Week>(context).budget.toString();
That part works perfect and I then can assign the value to the TextEditingController
TextEditingController currentBudgetController = TextEditingController(text: curentbudget);
After editing the value in the alert dialog, I then try to save the value with my update function:
onPressed: () {
//save new value
double budgetDouble =double.parse(currentBudgetController.text);
Provider.of<Week>(context, listen: false).updateBudget(newBudget: budgetDouble);
//close Dialog
Navigator.of(context).pop();
}
Again, not sure if it's not saving because it's a double
/ string
issue.
My Week Class update function looks like this:
//updateBudget
void updateBudget({
required double newBudget,
}) {
Week().budget = newBudget;
notifyListeners();
}
And the location I am trying to update the budget is:
child: Text(
Provider.of<Week>(context).budget.toString(),
style: const TextStyle(fontSize: 25.0),
),
Troubleshooting:
I stepped through with the debugger and when I update the value to let's say 300.0 I successfully get that value through the update function:
Week().budget = newBudget;
the next line is the notifyListeners();
and it looks like thats not updating the value in my view for some reason.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以像这样调用您的方法,
要获取可以使用
Consumer
小部件的值。You can call your method like this,
For getting the value you can use
Consumer
widget.