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());
}
CodePudding user response:
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..
CodePudding user response:
try using the below code
class EntryFields {
static late String id = '_id';
static late String name = '_name';
static late String navigation = '_navigation';
}
class Entries {
const Entries({
this.id,
this.name,
this.navigation,
});
final String? id;
final String? name;
final String? navigation;
Map<String, dynamic> toJson() => {
"_id": id,
"_name": name,
"_navigation": navigation,
};
}
Future<void> create(Entries entries) async {
final db = await instance.database;
final id = await db.insert(tableFavs, entries.toJson());
}
void main(){
final entriesFromField = Entries(
id: EntryFields.id,
name: EntryFields.name,
navigation: EntryFields.navigation
);
create(entriesFromField);
}
or better you can use this json generator