【问题标题】:ListView not updating from database changesListView 未从数据库更改中更新
【发布时间】:2020-05-18 08:57:45
【问题描述】:

非常感谢您的帮助,因为我这几天一直在尝试解决这个问题,但仍然无法解决问题。

下面的所有代码都可以完美运行,直到最近,列表停止更新,直到我热重启它。

下面两个函数分别获取我的地图列表和我的笔记列表

Future<List<Map<String, dynamic>>> getNoteListMap() async {
    Database db = await this.database;
    var result = await db.query(noteTable);
    return result;
  }


Future<List<Note>> getNoteList() async {
    var noteMapList = await getNoteListMap();
    int count = noteMapList.length;
    //list of notes, each note consist of their own independent variables
    List<Note> noteList = new List<Note>();
    for (int i = 0; i < count; i++) {
      noteList.add(Note.fromMapObject(noteMapList[i]));
    }
    return noteList;
  }

这是我的主要功能,它使用 FutureBuilder 创建一个列表。但是列表没有更新,只有在我每次热重启后才会更新。

FutureBuilder<List<Note>>(
  future: _databaseHelper.getNoteList(),
  builder: (BuildContext context, AsyncSnapshot<List<Note>> snapshot) {
    if (snapshot.hasData) {
      return ListView.builder(
        physics: TargetPlatform.iOS!=null? BouncingScrollPhysics() : ClampingScrollPhysics(),
        itemCount: snapshot.data.length,
        itemBuilder: (BuildContext context, int position) {
          Note note = snapshot.data[position];
          return Card(
            color: Colors.white,
            elevation: 2.0,
            child: new ListTile(
              title: new Text(note.title?? ''),
              subtitle: new Text(note.bodyText ?? ''),
              onTap: () => _navigateToTaskByDatePage(date),
            ),
          );
        }
      );
    } else if (snapshot.hasError) {
      return Container(
        child: Center(
          child: new Text('Error: ${snapshot.error}'),
        ),
      );
    } else {
      return Container(
        width: 0,
        height: 0,
      );
    }
  },
),

【问题讨论】:

  • 您的代码似乎没有任何问题。由于我们无权访问您的数据库,因此很难帮助您进行调试。您是否尝试过对AsyncSnapshotConnectionState 进行检查以查看发生了什么?
  • 会调查一下,谢谢你的建议。
  • 您是否尝试过使用StreamBuilder 而不是FutureBuilderHere's 一个关于两者区别的问题和答案。
  • 在你调用函数后尝试调用setState()方法,它会改变应用状态。

标签: listview flutter


【解决方案1】:

这个怎么样?

class ... {
  Future<List<Note>> futureNoteList;

  @override
  void initState() {
    super.initState();
    futureNoteList = _databaseHelper.getNoteList();
  }

  FutureBuilder<List<Note>>(
    future: futureNoteList,
    ...
  ),

  void update() async {
    val newList = await _databaseHelper.getNoteList();
    setState(() {
      futureNoteList = newList;
    });
  }

【讨论】:

    【解决方案2】:

    感谢您提供的所有解决方案,以帮助解决我面临的问题!但是,我设法通过进行颤振升级来解决我的问题。显然我的代码没有问题,只是flutter版本的问题。

    【讨论】:

      猜你喜欢
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2019-10-25
      • 2016-03-02
      相关资源
      最近更新 更多