Search code examples
fluttersqflitedata-storage

Flutter SQFlite Class to Json()


I am currently working on an app where the user is able to store data on their device locally. Therefor I am using the sqflite package but I am running into some errors converting my Class data into Json. This is the error message I get:

A value of type 'Set' can't be returned from the method 'toJson' because it has a return type of 'Map<String, Widget>'. due to this line:

Map<String, Widget> toJson() => {
            EntryFields.id = id,
            EntryFields.name = name,
            EntryFields.navigation = navigation,
          };

This is my class:

import 'package:flutter/material.dart';

const String tableFavs = 'favorites';

class EntryFields {
  static late String id = '_id';
  static late String name = '_name';
  static late String navigation = '_navigation';
}

class Entries {
  final int id;
  final String name;
  final Widget navigation;

  Entries({
    required this.id,
    required this.name,
    required this.navigation,
  });

  Map<String, Widget> toJson() => {
        EntryFields.id = id,
        EntryFields.name = name,
        EntryFields.navigation = navigation,
      };
}

and this is a snipped from my database:

  Future<Entries> create(Entries entries) async {
    final db = await instance.database;

    final id = await db.insert(tableFavs, entries.toJson());
  }

Solution

  • you can't store a widget in the database it should be Map<String, String> try to store the parameters of the widget as a String, not the whole widget you can store these types double, string, int, bool..