Home > Back-end >  Flutter The return type 'Map<int, int>' isn't a 'void', as required b
Flutter The return type 'Map<int, int>' isn't a 'void', as required b

Time:12-13

Hi i want to return a map from a FirebaseDatabase. But i get the Error Code:

The return type 'Map<int, int>' isn't a 'void', as required by the closure's context.

if i print the map i get the right result. Im new in Flutter and i dont get it why its doesnt work. I guess i need to change the method type, but how?


  String u1= 'Backsquat';

  Dataread(String u1);

  DatabaseReference data = FirebaseDatabase.instance.reference();
  FirebaseAuth auth = FirebaseAuth.instance;



  Map? read()  {
    Map <int,int> werte;

    data.child("Kraftwerte").child(auth.currentUser.uid).child(u1).onValue.listen((event) {
      werte = event.snapshot.value;
      print(werte);
      return werte;
    }); ```

CodePudding user response:

The error comes from the fact that your stream is asynchronous, while your actual function is synchronous. If all you want is to return event.snapshot.value for every item on your stream, you can do this:

Stream<Map?> read() {
  return data.child("Kraftwerte").child(auth.currentUser.uid).child(u1).onValue.map<Map>((event) => event.snapshot.value);
}

If what you want is to get the first value of the stream:

Future<Map?> read() async {
  final event = await data.child("krafwerte").child(auth.currentUser.uid).child(u1).onValue.first;

  return event.snapshot.value as Map?;
}

Either way, your code must be async

  • Related