I’m trying to send data to a Firebase Realtime database, but when I’m trying to write I get an error that says [firebase_database/unknown] Could not convert
.
Here is the stack tree:
[firebase_database/unknown] Could not convert
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 288:49 throw_
packages/firebase_database_web/src/interop/utils/utils.dart 92:5 jsify
packages/firebase_database_web/src/interop/utils/utils.dart 80:36 <fn>
dart-sdk/lib/_internal/js_dev_runtime/private/linked_hash_map.dart 21:13 forEach
packages/firebase_database_web/src/interop/utils/utils.dart 79:15 jsify
packages/firebase_database_web/src/interop/database.dart 142:53 set
packages/firebase_database_web/src/database_reference_web.dart 51:23 set
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54 runBody
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 123:5 _async
packages/firebase_database_web/src/database_reference_web.dart 49:19 set
packages/firebase_database/src/database_reference.dart 73:22 set
packages/ziique/FireService/RealtimeData/fire_beatIt_realtime_service.dart 34:23 createSession
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 45:50 <fn>
dart-sdk/lib/async/zone.dart 1661:54 runUnary
dart-sdk/lib/async/future_impl.dart 147:18 handleValue
dart-sdk/lib/async/future_impl.dart 784:44 handleValueCallback
dart-sdk/lib/async/future_impl.dart 813:13 _propagateToListeners
dart-sdk/lib/async/future_impl.dart 584:5 [_completeWithValue]
dart-sdk/lib/async/future_impl.dart 657:7 callback
dart-sdk/lib/async/schedule_microtask.dart 40:11 _microtaskLoop
dart-sdk/lib/async/schedule_microtask.dart 49:5 _startMicrotaskLoop
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 177:15 <fn>
And here is my code:
DatabaseReference ref = FirebaseDatabase.instance.ref();
var uuid = const Uuid();
List<User> users = [];
int timeschanged = 0;
int timesplayed = 0;
int versionID = 1;
Future<void> createSession(Beat beat, User host) async {
String id = uuid.v4();
try {
BeatItSession beatItSession = BeatItSession(
sessionid: id,
usersadded: users,
creationTime: DateTime.now(),
lastModified: DateTime.now(),
beatString: beat.beatString,
timeschanged: timeschanged,
timesplayed: timesplayed,
hostID: host.uid,
versionid: versionID);
final sessionRef = ref.child("beatItSessions").child(id);
print(beatItSession.toMap());
print(ref.path);
await sessionRef.set(beatItSession.toMap());
} catch (e) {
if (kDebugMode) {
print(e);
}
}
}
I have edited the rules on firebase to allow all read.
I have also tried to write manual json code with a simple name and still got the error.
I have tried giving the ref a path but got the same issue.
How can I solve this?
2
Answers
I Have just figured out the cause of the error. Objects. Firebase realtime does not like Objects.
soo my list with objects could not exists. had to use thier ID´s instead.
Firebase Realtime Database is a NoSQL database that stores data in a hierarchical JSON-like structure – so the above error will come up when you try save a non-JSON-like object.
All your objects need to be converted to JSON – freezed is a popular code generating package that is used to model data and it enables json conversions with the help of json_serializable. In your case it could look something like this:
There are many tutorials to help you set it up, such as this one.