简体   繁体   中英

Drift/Moor/Flutter StateError (Bad state: No element) when selecting single row with getSingle()

When retrieving data from a Drift database using the .getSingle() method, if there is no row matching the search criterion, a StateError (Bad state: No element) error is thrown. Is this expected behavior?

  Future<MyData> singleMyData(String id) {
    return (select(myDatas)..where((t) => t.id.equals(id)))
        .getSingle();
  }

...

var singleData = await myDatabase.singleMyData("theId");

It means that the element is not in the database. Try this instead:

 // can return empty list Future<List<DATA>> lookForSong(String songId) { return (select(records)..where((tbl) => tbl.uid.equals(recordId))).get(); } // data is sure to be present Future<DATA> getSong(String songId) { return (select(records)..where((tbl) => tbl.uid.equals(recordId))).getSingle(); }

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM