Null Exception in Flutter on Widget that seems to Print values

39 Views Asked by At

The problem is that I get an exception when I lunch my Application and I don't know why. It seems like it's trying to access a null variable. I've no idea where is the problem.

This is the code of Home class.

class Home extends StatelessWidget {
  final AuthService _auth = AuthService();

  Home({super.key});


  @override
  Widget build(BuildContext context) {
    return StreamProvider<QuerySnapshot?>.value(
      value: DataBaseService().events,
      initialData: null,
      child: Scaffold(
        backgroundColor: Colors.brown[300],
        appBar: AppBar(
          title: Text("Home Title"),
          elevation: 0.0,
          backgroundColor: Colors.brown[500],
          actions: <Widget>[
            TextButton(
              onPressed: () {},
              child: Icon(Icons.person),
            ),
            TextButton(
              onPressed: () async {
                await _auth.signOut();
              },
              child: Icon(Icons.logout),
            )
          ],
        ),
        body: EventsList(),
      ),
    );
  }
}

Here the code of EventsList class:

class _EventsListState extends State<EventsList> {
  @override
  Widget build(BuildContext context) {
    
    final events = Provider.of<QuerySnapshot?>(context);
    print(events);
    for (var e in events!.docs) {
      final data = e.data() as Map<String, dynamic>;
      print(data['eventName']);
      print(data);
    }
    return Container();
  }
}

(If you notice ... I try to Print the data related to the user and it works. Really do not know why when I enter in EventsList() I've got the error.

I put also the DataBaseService class:

class DataBaseService {
  final String? userID;

  DataBaseService({this.userID});

  final CollectionReference eventsCollection =
      FirebaseFirestore.instance.collection('event');

  Future updateUserData(String eventName, double cost, String age) async {
    return await eventsCollection
        .doc(userID)
        .set({'eventName': eventName, 'cost': cost, 'age': age});
  }

  Stream<QuerySnapshot>? get events {
    return eventsCollection.snapshots();
  }
}

And my Firestore Database:

Here the image

And also the image of the terminal (with the exception) The exception

I've searched for hours on sites, but any try did not worked. Do you have and idea how to fit it?

Thanks a lot!

1

There are 1 best solutions below

3
On

Ok, I removed the question mark and the exlamation point and the code now is:

class _EventsListState extends State<EventsList> {
  @override
  Widget build(BuildContext context) {
    //questa è la raccolta madre
    final events = Provider.of<QuerySnapshot>(context);
    print(events);
    for (var e in events.docs) {
      final data = e.data() as Map<String, dynamic>;
      print(data['eventName']);
      print(data);
    }
    return Container();
  }
}

Now I have this issue: new problems

It shouldn't happen because in the terminal I can see the value (the data) of events printed.

And ideas? It's like the Provider doesn't match the documents that are stored in FireStore.

Thank you very much!