I found the cause of the updating problem using Sqlite. It's related with widget.id
in void function. Because function is not a widget, so widget.id
is always called null
. Is there a way to solve it or something that can be used instead of widget.id
? Thanks in advance.
Widget vocaBuilder() {
return FutureBuilder(
builder: (context, snap) {
return ListView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemCount: snap.data.length,
scrollDirection: Axis.vertical,
itemBuilder: (context, index) {
Voca voca = snap.data[index];
return GestureDetector(
onTap: () {
editPage(voca.id);
},
...
}
void editPage(String id){
...
Padding(
padding:
EdgeInsets.only(top: 20, left: 270, bottom: 5),
child: FlatButton(
child: Text('update',
style: TextStyle(fontSize: 22,
fontWeight: FontWeight.w800)),
onPressed: () {
setState(() {
updateDB();
});
})),
FutureBuilder<List<Voca>>(
future: loadEditVoca(id),
builder: (BuildContext context,
AsyncSnapshot<List<Voca>> snapshot) {
if (snapshot.data == null ||
snapshot.data == []) {
return Container();
} else {
Voca voca = snapshot.data[0];
word = voca.word;
final TextEditingController tecWord =
TextEditingController();
tecWord.text = voca.word;
final TextEditingController tecMeaning =
TextEditingController();
tecMeaning.text = voca.meaning;
meaning = voca.meaning;
createTime = voca.createTime;
....
)));
});
});
}
}
Future<void> updateDB() async {
DBHelper sd = DBHelper();
var fido = Voca(
id: widget.id,// => always null. need to fix it.
word: this.word,
meaning: this.meaning,
createTime: this.createTime);
await sd.updateVoca(fido);
print(await sd.vocas());
Navigator.pop(_context);
}
You can pass id
from editPage
to updateDB
.
updateDB(id);
And for your updateDB
function
Future<void> updateDB(String id) async {
...
var fido = Voca(
id: id, // => use the id parameter
word: this.word,
meaning: this.meaning,
createTime: this.createTime);
.....
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.