1

In the sqlite table i have null columns. I made a query to get all data from specific column and map its data into List<Map<String, dynamic>>:

List<Map<String, dynamic>> maps = List();
final db = await database;
var result = await db.query('tools_table', where: 'LifeCycle <> 2');
result.forEach((val) {
  maps.add(val);
});

but null column converted into the "null" and when i want to deserialize list of map into object :

ToolsModel.fromJson(Map<String, dynamic> json) {
...
    if (json['triggerImagesUrl'] != null) {
      triggerImagesUrl = List<String>();
      var job = jsonDecode(json['triggerImagesUrl']);
      job.forEach((v) {
        triggerImagesUrl.add(v);
      });
    }

I got error because json['TriggerUrl'] is nut null, it is "null" and var job = jsonDecode(json['TriggerUrl']); job be a null and job.forEach cause e exception.

Why null column is converted to the "null" and how can i prevent of this action ?

12
  • Can you share the schema and the insertion query? Commented May 3, 2020 at 10:31
  • paste.ubuntu.com/p/fM7X32PPKb @snj Commented May 3, 2020 at 10:54
  • Is TriggerUrl changed to TriggerImagesUrl? Commented May 3, 2020 at 11:00
  • yes i changed @snj Commented May 3, 2020 at 11:00
  • please share JSON response too. It can also be the issue Commented May 3, 2020 at 11:00

1 Answer 1

1

Try this in dart pad

import 'dart:convert';
void main() {
  List<String> urls = null;
  var d = jsonEncode(urls);
  print(d  is String);
  print(jsonDecode(d) is String);
}

You'll get following output

true
false

So jsonEncode(urls) returns null string if urls is null.
Change this

Map<String, Object> mapToCacheActivatedToolItem(LogData.Data item) {
    var newMap = {
      "TriggerImagesUrl": item.triggerImagesUrl == null ? null : jsonEncode(urls)
    };
    return newMap;
  }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.