'Error: The argument type 'Null Function(DataSnapshot)'

import 'package:flutter/material.dart';
import 'package:firebase_database/firebase_database.dart';

class FirebaseConsultaPage extends StatefulWidget {
  const FirebaseConsultaPage({Key? key}) : super(key: key);

  @override
  _FirebaseConsultaPage createState() => _FirebaseConsultaPage();
}

class _FirebaseConsultaPage extends State<FirebaseConsultaPage> {
  var users = [];

  // ignore: deprecated_member_use
  final dbRef = FirebaseDatabase.instance.reference();

  consultar() {
    dbRef.child('users').once().then((DataSnapshot snapshot) {
      print(snapshot.value);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('FireBase Consulta'),
        ),
        body: Column(children: [
          RaisedButton(
            child: Text('Consultar'),
            onPressed: consultar
          )
        ]
        )
      );
  }
}

when trying to execute the "consultar" function returns the error:

*: Error: The argument type 'Null Function(DataSnapshot)' can't be assigned to the parameter type 'FutureOr Function(DatabaseEvent)'. lib/…/firebase/query.dart:18

  • 'DataSnapshot' is from 'package:firebase_database/firebase_database.dart'*

how can i fix it??enter image description here



Solution 1:[1]

  consultar() {
    dbRef.child('users').once().then((event) {
      print(event.snapshot.value);
    });
  }

.then will return a DatabaseEvent event not a DataSnapshot snapshot and you should access the snapshot using event.snapshot

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ali Ammar