扑来:当TextField值更改时,应用程序冻结
因此,我正在尝试创建一个可以编辑输入字段的应用程序,当TextFields(输入)中的值更改时,它将为您计算一些值。为了实现这一目标,我正在尝试使用提供商来保留其值,设定器和获取器,但是每当使用Setter时,所有Freezez都可以使用。
从顶部使用MytialsApp的设置,使用多个小部件和添加提供商(虽然目前只有一个):
return MultiProvider(
providers: [
ChangeNotifierProvider<CalculationService>(create: (_) => calculationService),
],
child: Builder(
builder: (context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Palette.primary,
scaffoldBackgroundColor: const Color(0xFFF8F9FA),
),
home: RootScreen(),
);
},
),
);
我的“ Rootscreen”然后获得具有值和getters和setters的计算服务(set set reageinmetersquared(int state state state state state state state )
是我相信的重要部分):
class CalculationService with ChangeNotifier {
late final SharedPreferences sharedPreferences;
// Inputs
final int _areaInMeterSquared = 0;
final int _basicRentForUnit = 0;
final int _installmentsPerYear = 0;
final int _tenantCustomizations = 0;
final int _technicalUpgrades = 0;
CalculationService(this.sharedPreferences);
final double rateOfReturn = 0.02;
final double inflation = 0.05;
final int tenantCustomizationYears = 5;
final int technicalUpgradeYears = 20;
int get areaInMeterSquared => _areaInMeterSquared;
int get basicRentForUnit => _basicRentForUnit;
int get installmentsPerYear => _installmentsPerYear;
int get tenantCustomizations => _tenantCustomizations;
int get technicalUpgrades => _technicalUpgrades;
set areaInMeterSquared(int state) {
sharedPreferences.setInt(areaInMeterSquaredKey, state);
areaInMeterSquared = state;
notifyListeners();
}
}
textfield(s)看起来像这样:
TextFormField(
initialValue: calculationService.areaInMeterSquared.toString(),
onChanged: (text) {
calculationService.areaInMeterSquared = int.parse(text);
},
),
谢谢您的任何帮助::)
编辑:我得到的错误是[errive:flutter/lib/ui/ui/ui_dart_state .cc(198)]未经手的例外:堆栈溢出
So I'm trying to create an app where you can edit input fields, and it will calculate some values for you when the values in the TextFields (inputs) changes. To achieve this I'm trying to use a provider to hold the values and setters and getters for them, but whenever the setter is used everything freezez up.
From the top my MaterialApp is set up like this using the MultiProvider widget and adding providers (currently only one though):
return MultiProvider(
providers: [
ChangeNotifierProvider<CalculationService>(create: (_) => calculationService),
],
child: Builder(
builder: (context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Palette.primary,
scaffoldBackgroundColor: const Color(0xFFF8F9FA),
),
home: RootScreen(),
);
},
),
);
My "RootScreen" then gets the calculationService which holds the values and getters and setters (the set areaInMeterSquared(int state)
is the important part here i believe):
class CalculationService with ChangeNotifier {
late final SharedPreferences sharedPreferences;
// Inputs
final int _areaInMeterSquared = 0;
final int _basicRentForUnit = 0;
final int _installmentsPerYear = 0;
final int _tenantCustomizations = 0;
final int _technicalUpgrades = 0;
CalculationService(this.sharedPreferences);
final double rateOfReturn = 0.02;
final double inflation = 0.05;
final int tenantCustomizationYears = 5;
final int technicalUpgradeYears = 20;
int get areaInMeterSquared => _areaInMeterSquared;
int get basicRentForUnit => _basicRentForUnit;
int get installmentsPerYear => _installmentsPerYear;
int get tenantCustomizations => _tenantCustomizations;
int get technicalUpgrades => _technicalUpgrades;
set areaInMeterSquared(int state) {
sharedPreferences.setInt(areaInMeterSquaredKey, state);
areaInMeterSquared = state;
notifyListeners();
}
}
And the TextField(s) looks like this:
TextFormField(
initialValue: calculationService.areaInMeterSquared.toString(),
onChanged: (text) {
calculationService.areaInMeterSquared = int.parse(text);
},
),
Thanks for any help :)
EDIT: The error i get is [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: Stack Overflow
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在计算服务中使用此文
本表单字段应该像这样
In CalculationService use this
The text form field should be like this