简体   繁体   中英

Handling Firebase Stream Object in Flutter

I'm trying to receive Data from the Firebase Realtime Database in Flutter. I can't get it working - all the solutions I found so far don't work.

The connection to the database is working and getting Data:

String userid = FirebaseAuth.instance.currentUser!.uid;
DatabaseReference _database =FirebaseDatabase.instance.ref();

void _getData(){

  _itemStream = _database.child('users/$userid/items/').onValue.listen((event) {
  final Object? mydata = event.snapshot.value;


  print(mydata.toString());

});
}

This gives me my set of Data as an Object. The Print-Statement returns this, which is the correct Dataset for the node:

{1646396962722: {mhd: 2022-03-04 12:29:22.722218, name: Hund, startdate: 2022-03-04 12:29:22.722106, fach: 0}, 1646396401456: {mhd: 2022-03-04 12:20:01.461668, name: Hund, startdate: 2022-03-04 12:20:01.456343, fach: 0}, 1646396946311: {mhd: 2022-03-04 12:29:06.312466, name: Maus,  startdate: 2022-03-04 12:29:06.311793, fach: 0}}

The problem I can't solve is: How to Loop through this Dataset and get it into a List in Dart. If I try something like

mydata?.forEach()

I get the Error:

The method 'forEach' isn't defined for the type 'Object'.

Im really stuck. Maybe anyone can give me a tip how to approach this.

Thanks!

only sketchup code:

final List<dynamic> list = myData as List<dynamic>;
for(final dynamic item of list) {
final Map<String, dynamic> dataAsMap = item as Map<String, dynamic>
print(item.keys);
final Map<String, dynamic> actualObject = map[item.keys.first as String] as Map<String, dynamic>;
print(actualObject["name"]);
}
}

If you look at your dataset, this is in fact a Map<dynamic, dynamic> . You need to convert this to a better Dart object or use the for in loop. see the example below:

main() {
// in your example 
//  final Map<dynamic, dynamic>? mydata = event.snapshot.value;
  final Map<dynamic, dynamic> yourMap = {
    '1646396962722': {
      'mhd': '2022-03-04 12:29:22.722218',
      'name': 'Hund',
      'startdate': '2022-03-04 12:29:22.722106',
      'fach': '0'
    },
    '1646396401456': {
      'mhd': '2022-03-04 12:20:01.461668',
      'name': 'Hund',
      'startdate': '2022-03-04 12:20:01.456343',
      'fach': '0'
    },
    '1646396946311': {
      'mhd': '2022-03-04 12:29:06.312466',
      'name': 'Maus',
      'startdate': '2022-03-04 12:29:06.311793',
      'fach': '0'
    }
  };

  for (final entry in yourMap.entries) {
    print('key: ${entry.key}, value: ${entry.value}');
  }
}
//key: 1646396962722, value: {mhd: 2022-03-04 12:29:22.722218, name: Hund, startdate: 2022-03-04 12:29:22.722106, fach: 0}
//key: 1646396401456, value: {mhd: 2022-03-04 12:20:01.461668, name: Hund, startdate: 2022-03-04 12:20:01.456343, fach: 0}
//key: 1646396946311, value: {mhd: 2022-03-04 12:29:06.312466, name: Maus, startdate: 2022-03-04 12:29:06.311793, fach: 0}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM