0

I am receiving a null value when I am saving a value as in my code I have three fields

   String? _itemName ="";
   String? _dateCreated ="";
   int? _id; 

and when after running my project, I insert an item by calling this method:

Future<int> saveItem(NoDoItem item) async
{
    var dbClient = await database;
    int res = await dbClient.insert(tableName, item.toMap());
    debugPrint(res.toString());
    return res;
}

Using saveItem() function:

void _hndleSubmitted(String text) async
  {
      _textEditingController.clear();

      NoDoItem noDoItem = NoDoItem(text, DateTime.now().toIso8601String());
      int savedItemId = await db.saveItem(noDoItem);

      debugPrint("Item saved ID: $savedItemId");
 }

and after this when I am retrieving all itemNames, I get null value instead of user entered value

Defining getItems() func:

Future<List> getItems() async
{
    var dbClient = await database;
    var result = await dbClient.rawQuery("SELECT * FROM $tableName ORDER BY $columnItemName ASC");
    return result.toList();
}

Using getItems() function:

 _readNotoDoItems() async
  {
    List items = await db.getItems();
    items.forEach((item) {
      NoDoItem noDoItem = NoDoItem.map(item);
      print("Db items:  ${noDoItem.itemName}");
    });
  }

Please tell me what is the the reason that I am getting a null value instead of entered value and how to fix this issue?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Saad Ebad
  • 206
  • 5
  • 16
  • 1
    Did you check what is in your database? – nvoigt Oct 12 '21 at 16:42
  • Does this answer your question? [Null value is saving instead of entered value in Flutter](https://stackoverflow.com/questions/69541212/null-value-is-saving-instead-of-entered-value-in-flutter) – marc_s Oct 13 '21 at 16:07

1 Answers1

0

I think the problem is here

void _hndleSubmitted(String text) async {
    _textEditingController.clear(); // <--------- HERE

      NoDoItem noDoItem = NoDoItem(text, DateTime.now().toIso8601String());
      int savedItemId = await db.saveItem(noDoItem);

      debugPrint("Item saved ID: $savedItemId");
 }

you clear the value with _textEditingController.clear(); right before using it. move it after adding the item to database

void _hndleSubmitted(String text) async {
     NoDoItem noDoItem = NoDoItem(text, DateTime.now().toIso8601String());
     int savedItemId = await db.saveItem(noDoItem);
     textEditingController.clear();
     debugPrint("Item saved ID: $savedItemId");
  }
Fadel
  • 195
  • 1
  • 9