我有一个textfield
,并且我在我的应用中使用sqflite
数据库。 sqflite
的值需要分配给我的textfield
这是我的textfield
代码
StreamBuilder<String>(
stream: patientHealthFormBloc.doctorName,
builder: (context, snapshot) {
return TextFormField(
initialValue: patientHealthFormBloc.doctorNameValue,
onChanged: (value) {
patientHealthFormBloc.doctorNameChanged(value);
},
...
现在在我的课程的initstate
方法中,我正在从数据库中获取值。
这是异步操作,因此需要时间。
我的bloc类具有如下代码
Function(String) get doctorNameChanged => _doctorName.sink.add;
所以一旦我从数据库中获得价值,我就会立即致电
doctorNameChanged("valuefromdatabase");
但是我在文本字段中看不到该值。我的数据库中也有一个值。
是否可以不使用TextEditingController
或setState
来更新值。我试图避免这些问题,因为我的课程分为很多小块,而且使用上述任何方法都过于复杂
我尝试对RadioButton
和CheckBox
使用相同的方法,它们似乎已正确更新。
该值也在数据库中存在的_doctorName.stream.value
中更新,但是textfield
不显示任何数据。我也尝试过更改textfield
的颜色,所以那里没有问题,而且我能够看到输入的内容。
我已经对应用https://github.com/PritishSawant/demo/tree/master/lib作了一个小型演示
我使用sqflite
而不是使用shared preferences
,但是问题仍然存在
答案 0 :(得分:4)
好的,所以我终于找到了解决问题的方法。
以下是我的代码,在下面的示例中,我仅使用SharedPreferences
而不是sqflite
。sqflite
class _MyHomePageState extends State<MyHomePage> {
MyBloc myBloc = MyBloc();
TextEditingController myController = TextEditingController();
@override
void dispose() {
myBloc?.close();
myController?.dispose();
super.dispose();
}
@override
void initState() {
super.initState();
AppPreferences.setString("data", "this is my data");
AppPreferences.getString("data").then((value){
myBloc.dataChanged(value);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: StreamBuilder(
stream: myBloc.data,
builder: (context,snapshot){
debugPrint(snapshot.data);
myController.value = myController.value.copyWith(text: myBloc.dataValue);
return TextFormField(
controller: myController,
onChanged: (value){
myBloc.dataChanged(value);
},
);
},
),
),
);
}
}
答案 1 :(得分:2)
尝试以下方法:
StreamBuilder<String>(
stream: patientHealthFormBloc.doctorName,
builder: (context, snapshot) {
String doctorName = patientHealthFormBloc.doctorNameValue;
if(snapshot.hasData){
doctorName = snapshot.data/*(your name string from the stream)*/;
}
return TextFormField(
initialValue: doctorName,
onChanged: (value) {
patientHealthFormBloc.doctorNameChanged(value);
},
...
让我知道是否需要更多帮助。
答案 2 :(得分:1)
我的评论所暗示的是这样的:
"""You are given an insect moves in a plane starting from the original point (0,0).
The insect that can only move toward North-East, South-East, North-West, & South-West.
Now the thing is insect can't move more than a specific number of steps at a time in one direction.
For example, it can move only 3 steps in NE, 4 steps in SE, 5 steps in NW and 2 steps in SW.
If the insect moves 3 steps in direction NE at a stretch- it has to move NW, SE, or SW-
before again going in direction NE."""
"""Assumption say the insect moves in the plane from (-100,-100) to (100, 100) size plane
North is Positive y-cord
South is Negative y-cord
East is Positive x-cord
West is Negative x-cord """
def walk_ne():
max_ne = 3
global x_curr
global y_curr
global x_home
global y_home
global walked_ne
global walked_nw
global walked_se
global walked_sw
walked_ne = walked_nw = walked_sw = walked_se = False
walked_ne = True
for i in range(max_ne):
if x_curr < x_home and y_curr < y_home:
x_curr += 1
y_curr += 1
elif x_curr == x_home and (y_curr + 1) < y_home:
x_curr += 1
y_curr += 1
break
elif y_curr == y_home and (x_curr + 1) < x_home:
x_curr += 1
y_curr += 1
break
# else:
# print("Cond did not meet")
def walk_nw():
max_nw = 5
global x_curr
global y_curr
global x_home
global y_home
global walked_ne
global walked_nw
global walked_se
global walked_sw
walked_ne = walked_nw = walked_sw = walked_se = False
walked_nw = True
for i in range(max_nw):
if x_curr < x_home and y_curr > y_home:
x_curr += 1
y_curr -= 1
elif x_curr == x_home and (y_curr - 1) > y_home:
x_curr += 1
y_curr -= 1
break
elif y_curr == y_home and (x_curr + 1) < x_home:
x_curr += 1
y_curr -= 1
break
# else:
# print("Cond did not meet")
def walk_se():
max_se = 4
global x_curr
global y_curr
global x_home
global y_home
global walked_ne
global walked_nw
global walked_se
global walked_sw
walked_ne = walked_nw = walked_sw = walked_se = False
walked_se = True
for i in range(max_se):
if x_curr > x_home and y_curr < y_home:
x_curr -= 1
y_curr += 1
elif x_curr == x_home and (y_curr + 1) < y_home:
x_curr -= 1
y_curr += 1
break
elif y_curr == y_home and (x_curr - 1) > x_home:
x_curr -= 1
y_curr -= 1
break
# else:
# print("Cond did not meet")
def walk_sw():
max_sw = 2
global x_curr
global y_curr
global x_home
global y_home
global walked_ne
global walked_nw
global walked_se
global walked_sw
walked_ne = walked_nw = walked_sw = walked_se = False
walked_sw = True
for i in range(max_sw):
if x_curr > x_home and y_curr > y_home:
x_curr -= 1
y_curr -= 1
elif x_curr == x_home and (y_curr - 1) > y_home:
x_curr -= 1
y_curr -= 1
break
elif y_curr == y_home and (x_curr - 1) > x_home:
x_curr -= 1
y_curr -= 1
break
# else:
# # print("Cond did not meet")
x_curr = 0 # Current x location of insect
y_curr = 0 # Current y location of insect
x_home = int(input('home x cordinates (Integer between -100 to 100)'))
y_home = int(input('home y cordinates (Integer between -100 to 100)'))
walked_ne = walked_se = walked_nw = walked_sw = False
if (x_home + y_home) % 2 != 0:
print('Nearest the insect can go to given coordinates is ({} {}) '
'Because insect can only move in diagonal'.format(x_home-1, y_home))
x_home -= 1
while x_home != x_curr and y_home != y_curr:
if x_curr < x_home:
if y_curr < y_home:
if not walked_ne:
walk_ne()
else:
x_curr += 1
y_curr -= 1
walked_ne = False
walked_nw = True
else:
if not walked_nw:
walk_nw()
else:
x_curr += 1
y_curr += 1
walked_nw = False
walked_ne = True
else:
if y_curr < y_home:
if not walked_se:
walk_se()
else:
x_curr -= 1
y_curr -= 1
walked_se = False
walked_sw = True
else:
if not walked_sw:
walk_sw()
else:
x_curr -= 1
y_curr += 1
walked_sw = False
walked_se = True
if x_curr == x_home and y_curr != y_home:
if y_curr < y_home:
if not walked_ne:
walk_ne()
elif not walked_se:
walk_se()
else:
if not walked_nw:
walk_nw()
elif not walked_sw:
walk_sw()
elif x_curr != x_home and y_curr == y_home:
if x_curr < x_home:
if not walked_ne:
walk_ne()
elif not walked_nw:
walk_nw()
else:
if not walked_se:
walk_se()
elif not walked_sw:
walk_sw()
print(x_curr, y_curr)
在不想理解为什么您不想使用TextEditingController或setState的情况下,我不想写这个答案。但这应该可以在使用Bloc模式时实现您想要的。