0

Using firebase_database: ^7.1.1

Before null safety i used to do like this to get the data from firebase database:

DataSnapshot dataSnapshot = await References.getReferenceYears().once();
Map<dynamic, dynamic> values = dataSnapshot.value;
values.forEach((key, value) {...}

Now when i try do like this, i get the error as bellow:

DataSnapshot? dataSnapshot = await References.getReferenceYears().once();
Map<dynamic, dynamic> values = dataSnapshot.value;

[ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: type 'List<Object?>' is not a subtype of type 'Map<dynamic, dynamic>'

I think this means that the keys are not being returned in the dataSnapshot.value, how can i use this now to get the (key, value) as before?

If I print the snapshot.value I get like this, so looks like the keys are no longer present in the snapshot.value as before, i don't know where are the keys now:

[{name: 2016-2017}, {name: 2017-2018}, {name: 2018-2019}]

Thanks in advance.

SuperStormer
  • 4,997
  • 5
  • 25
  • 35
Jorge Vieira
  • 2,784
  • 3
  • 24
  • 34
  • 1
    It sounds like you have sequential, numeric keys under `References.getReferenceYears()`, which the SDK converts to an array. I don't think this changed in a recent SDK, as Firebase always tries to coerce such keys into an array. If you're certain it changed, can you edit your question to show the JSON at `References.getReferenceYears()` (as text, no screenshots please)? You can get this by clicking the "Export JSON" link in the overflow menu (⠇) on your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen Jul 12 '21 at 18:32
  • Ahh yes, you're right. Thank you so much. – Jorge Vieira Jul 12 '21 at 19:47

4 Answers4

2

You probably update Flutterfire cloud_firestore too, becouse as you can see from the error, in the newest versione, the data returned from the snapshot is an Object or a List<Object> and not a map.

You can solve like this:

snapshot.value as Map<String, dynamic>

EDIT:

I saw you where referring to firebase realtime database. So the problem is not caused by flutter_fire.

The error tells you that you are trying to put a List into a Map<dynamic,dynamic>.

One solution could be:

Map<dynamic, dynamic> values = dataSnapshot.value as Map<dynamic, dynamic>;
L. Gangemi
  • 3,110
  • 1
  • 22
  • 47
2

Try this

DataSnapshot dataSnapshot = await References.getReferenceYears().once();

(dataSnapshot as Map<dynamic, dynamic>).forEach((key, value) {...}
Huthaifa Muayyad
  • 11,321
  • 3
  • 17
  • 49
1

It sounds like you have sequential, numeric keys under References.getReferenceYears(), which the SDK converts to an array. I don't think this changed in a recent SDK, as Firebase always tries to coerce such keys into an array.

If you don't want the values to be stored into an array, the easiest way to prevent that is to prefix the keys with a short non-numeric value, like:

"key_0": "value 0",
"key_1": "value 1",
"key_2": "value 2",
...
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

Use version 8.x.x for now as output type is dynamic instead of Object? in version 9.x.x

Map<dynamic, dynamic> mapData;
snapshot.values.forEach((key, value) {
  mapData[key][value];
})
VVV
  • 1