我具有通过列表中的id更新注释的功能,但是如果我更改值,地图列表将永远不会更新
addNoteItem(String id, String val) {
List<Map<String, String>> counterNoteItemList = _counterNoteItem.value;
counterNoteItemList
.where((test) => test.keys.toList()[0] == id)
.toList()[0]
.values
.toList()[0] = val;
print(
"counterNoteItemList : ${counterNoteItemList.where((test) => test.keys.toList()[0] == id).toList()[0]} ");
_counterNoteItem.sink.add(counterNoteItemList);
}
这是我的小部件代码,我使用文本字段并添加onchange按钮在bloc中调用我的函数并更新流,但是当我打印流时,它从未更新
Widget buildTextFieldNote(LaundryItems data, int index, int item) {
return StreamBuilder<List<Map<String, String>>>(
stream: _crBloc.counterNoteItem,
builder: (context, snapshot) {
if (!snapshot.hasData) return Container();
return Container(
padding: EdgeInsets.symmetric(horizontal: 15.0),
child: Theme(
data: ThemeData(
inputDecorationTheme: InputDecorationTheme(
border: UnderlineInputBorder(
borderSide: BorderSide(
color: constants.customColors.borderColor)),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: constants.customColors.borderColor)),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: constants.customColors.borderColor)))),
child: TextField(
keyboardType: TextInputType.multiline,
onChanged: (text) {
_crBloc.addNoteItem(
data.data.values.toList()[index][item].id, text);
},
maxLength: null,
maxLines: null,
),
),
);
});
}
我找到了一个解决方案,但我仍然觉得很奇怪,我认为有比这更好的解决方案了,
因此,当我想对特定列表进行更新时,我有一个包含Map<String, String>,
的列表,例如这样的列表内容
[
"12ab" : "i love u",
"22ab" : "i want u",
"33ab" : "i need u"
]
,我想将ID 12ab的值更新为“我非常爱你”,
我现在要做的是找到一个具有键12ab的对象,然后从列表中删除该对象,并使用新的Map<String, String>
addNoteItem(String id, String val) {
try {
List<Map<String, String>> counterNoteItemList = _counterNoteItem.value;
bool a = counterNoteItemList.remove(counterNoteItemList
.where((test) => test.keys.toList()[0] == id)
.toList()[0]);
Map<String, String> temp = {id: val};
counterNoteItemList.add(temp);
_counterNoteItem.sink.add(counterNoteItemList);
} catch (err) {}
}
答案 0 :(得分:0)
只需添加
setState(() {});
函数末尾,如
addNoteItem(String id, String val) {
List<Map<String, String>> counterNoteItemList = _counterNoteItem.value;
counterNoteItemList
.where((test) => test.keys.toList()[0] == id)
.toList()[0]
.values
.toList()[0] = val;
print(
"counterNoteItemList : ${counterNoteItemList.where((test) => test.keys.toList()[0] == id).toList()[0]} ");
_counterNoteItem.sink.add(counterNoteItemList);
setState(() {}); // Updates the Wigdet
}
答案 1 :(得分:0)
在类satefull扩展StatefulWidget中使用setState((){})
并添加类主体扩展状态
void Getpost(){
abstract class Element {
def contents: Array[String]
val someProperty: String = {
println("=== Element")
contents(0)
}
}
class UniformElement(
str: String
) extends Element {
val s = str
println("=== UniformElement.s " + s)
def contents = Array(s) // error
//def contents = Array(str) // ok
}
val e = new UniformElement("str")
println(e.someProperty)
}