颤振错误:正文可能正常完成,导致返回'null'


class Word {
int? id;
late String word;
late String description;
Word({required this.word, required this.description});
Word.withId({this.id, required this.word, required this.description});
Map<String,dynamic> toMap(){
var map = Map<String,dynamic>();
map["word"] = word;
map["description"] = description;
if (id!=null) {
map["id"] = id;
}
}

。..

Future<int?> insert(Word word) async {
/// db ekleme sorgusu
Database? db = await this.db;
/// database erişim
var result = await db!.insert("words", word.toMap());
return result;
}

我得到这个错误,我正在使用地图的这个代码块,有人能帮助吗?😕

error:主体可能正常完成,导致返回'null',但返回类型可能是不可空的类型。(body_might_complete_normal at [sqflite_demo] libmodelsword.dart:9)

这个函数的返回类型是Map<String, dynamic>:

Map<String,dynamic> toMap(){
var map = Map<String,dynamic>();
map["word"] = word;
map["description"] = description;
if (id!=null) {
map["id"] = id;
}
}

但是这个函数没有返回任何东西,它只是将这个Map赋值给一个变量map。你想在你的函数底部添加一行return map;,事情应该像你想要的那样工作。

Map<String,dynamic> toMap(){
var map = Map<String,dynamic>();
map["word"] = word;
map["description"] = description;
if (id!=null) {
map["id"] = id;
}
return map;
}

最新更新