【问题标题】:Flutter error: The body might complete normally, causing 'null' to be returnedFlutter 错误:主体可能正常完成,导致返回“null”
【发布时间】:2021-10-28 13:47:46
【问题描述】:
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;
  }

我在将 Map 用于此代码块时遇到此错误,有人可以帮忙吗? ????

错误:主体可能正常完成,导致返回“null”,但返回类型可能是不可为空的类型。 (body_might_complete_normally at [sqflite_demo] lib\models\word.dart:9)

【问题讨论】:

    标签: android ios flutter dart


    【解决方案1】:

    这个函数的返回类型是Map&lt;String, dynamic&gt;

      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;
      }
    

    【讨论】:

    • 它不起作用:(
    • 您是否遇到了新错误?
    • 是的,这里给出了错误 var result = await db!.insert("words", word.toMap());但是当我降低颤振的版本时我解决了这个问题:D
    猜你喜欢
    • 2022-10-01
    • 2021-06-26
    • 1970-01-01
    • 2022-11-18
    • 2021-11-15
    • 2021-09-29
    • 2021-11-21
    • 2022-08-06
    • 1970-01-01
    相关资源
    最近更新 更多