0

我正在使用 SQLITE db 构建一个应用程序,当列表中的项目更新时,我需要重绘屏幕。但不幸的是,它复制了数据库中的现有数据,当我重新启动应用程序时,它只显示更新的数据。这是我的 _readalldata 代码:

_readNoDoList() async {
 List items = await db.getItems();
  items.forEach((item) {
   // NoDoItem noDoItem = NoDoItem.fromMap(item);
    setState(() {

        _itemList.add(NoDoItem.map(item));
    });
   // print("Db items: ${noDoItem.itemName}");
  });

我打电话给:

FlatButton(
         onPressed: () async {
           NoDoItem newItemUpdated = NoDoItem.fromMap(
               {
                 "itemName": _textEditingController.text,
                 "dateCreated" : dateFormatted(),
                 "id" : item.id
               });

            
            await db.updateItem(newItemUpdated); //updating the item
            setState(() {
              _readNoDoList(); // redrawing the screen with all items saved in the db
            });

           // Navigator.pop(context);

         },
         child: new Text("Update")),

但是单击更新按钮后但重新启动应用程序之前的屏幕截图如下:[1]:https ://i.stack.imgur.com/DNg8K.png

并且重启app后是:[2]:https ://i.stack.imgur.com/pIrR5.png

我在哪里犯了错误?

4

1 回答 1

0

我不知道代码的其余部分是什么样的,但从我在这里看到的情况来看,_itemList每次从数据库中读取所有行时,您都会继续添加新项目。这样,您的列表开始包含重复的行,因为每次读取数据库时它们都会添加到已经存在的列表中。

可能的解决方案:尝试在功能打开后重置列表以仅保留新鲜结果:

  _readNoDoList() async { 
    _itemList = [];
  ...
  ...
于 2020-06-28T16:00:08.100 回答