簡體   English   中英

Flutter 錯誤:body 可能正常完成,導致返回 'null'

[英]Flutter error: The body might complete normally, causing 'null' to be returned

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 時遇到此錯誤,有人可以幫忙嗎? 😕

錯誤:主體可能正常完成,導致返回“空”,但返回類型可能是不可為空的類型。 (body_might_complete_normally 在 [sqflite_demo] lib\\models\\word.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;
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM