title: Retrieving documents from a collection and storing the details as objects in a list in Flutter
body: <p>I am trying to store details of events that users have created (which are stored in Firestore) and use them to populate cards. I have implemented a class that retrieves and stores the values in a list of objects. The issue I am coming across is when I try to access some of the objects I get a null and the widget populating the cards throws an error message.</p>
  <p><a href="https://i.stack.imgur.com/o1kpQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/o1kpQ.png" alt="called on null" /></a></p>
  <p>The code used for retrieving and storing the documents is:</p>
  <pre class="lang-dart prettyprint-override"><code>import 'package:cloud_firestore/cloud_firestore.dart';
  
  CollectionReference events = FirebaseFirestore.instance.collection(&quot;Events&quot;);
  DocumentReference eventDocs = events.doc();
  List&lt;dynamic&gt; userEventsList;
  // List&lt;Event&gt; firebaseEvents = &lt;Event&gt;[];
  
  class Event {
    final String eventName, eventID, location, duration, description, creatorID, imagePath;
    String category;
    final List categoryID;
  
    Event(
        {
          this.imagePath,
          this.eventName,
          this.eventID,
          this.location,
          this.duration,
          this.description,
          this.creatorID,
          this.categoryID,
          // this.galleryImages,
        }
        );
  }
  Future&lt;void&gt; getData() async {
    final eventsList = [];
  
    Stream&lt;QuerySnapshot&gt; eventDocs = events.snapshots();
  
    eventDocs.forEach((element) {
      element.docs.asMap().forEach((key, value) {
  
        final firebaseEventDocs = new Event(
            imagePath: &quot;assets/event_images/default_image.jpeg&quot;,
            eventName: element.docs[key][&quot;eventName&quot;],
            description: element.docs[key][&quot;description&quot;],
            location: element.docs[key][&quot;location&quot;],
            duration: element.docs[key][&quot;duration&quot;],
            categoryID: [0,element.docs[key][&quot;category&quot;]]);
  
        eventsList.add({
          firebaseEventDocs,
        });
      });
    });
    userEventsList = eventsList;
    print(&quot;The list is &quot; + userEvents.toString());
  }
  
  final userEvents = userEventsList;
  </code></pre>
  <p>Any ideas on what the issue could be?</p>
  <p>Edit:
  The 'where' is in my HomePage:</p>
  <pre class="lang-dart prettyprint-override"><code>import 'package:cloud_firestore/cloud_firestore.dart';
  import 'package:firebase_auth/firebase_auth.dart';
  import 'package:flutter/material.dart';
  import 'package:localeventsapp/Screens/Login/login_screen.dart';
  import 'package:localeventsapp/model/category.dart';
  import 'package:localeventsapp/model/event.dart';
  import 'package:localeventsapp/styleguide.dart';
  import 'package:localeventsapp/ui/event_details/event_details_page.dart';
  import 'package:localeventsapp/ui/homepage/form_widget.dart';
  import 'package:provider/provider.dart';
  import '../../app_state.dart';
  import '../../authentication_service.dart';
  import 'category_widget.dart';
  import 'event_widget.dart';
  import 'home_page_background.dart';
  
  CollectionReference users = FirebaseFirestore.instance.collection(&quot;Users&quot;);
  
  // CollectionReference usersEvents = FirebaseFirestore.instance.collection(&quot;Events&quot;);
  
  FirebaseAuth auth =  FirebaseAuth.instance;
  String uid = auth.currentUser.uid.toString();
  
  
  class MainPage extends StatefulWidget{
    HomePage createState()=&gt; HomePage();
  }
  
  class HomePage extends State&lt;MainPage&gt; {
  
  
    // Future&lt;void&gt; getData() async {
    //   // Get docs from collection reference
    //   QuerySnapshot querySnapshot = await usersEvents.get();
    //
    //   // Get data from docs and convert map to List
    //   final allData = querySnapshot.docs.map((doc) =&gt; doc.data()).toList();
    //
    //   print(allData);
    // }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: ChangeNotifierProvider&lt;AppState&gt;(
          create: (_) =&gt; AppState(),
          child: Stack(
            children: &lt;Widget&gt;[
              HomePageBackground(
                screenHeight: MediaQuery.of(context).size.height,
              ),
              SafeArea(
                child: SingleChildScrollView(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: &lt;Widget&gt;[
                      Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 32.0),
                        child: Row(
                          children: &lt;Widget&gt;[
                            Text(
                              &quot;TuLink&quot;,
                              style: fadedTextStyle,
                            ),
                            Spacer(),
                          ],
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 32.0),
                        child: Text(
                          &quot;Welcome!&quot;,
                          style: whiteHeadingTextStyle,
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.symmetric(vertical: 24.0),
                        child: Consumer&lt;AppState&gt;(
                          builder: (context, appState, _) =&gt;
                            SingleChildScrollView(
                            scrollDirection: Axis.horizontal,
                            child: Row(
                              children: &lt;Widget&gt;[
                                for (final category in categories)
                                  CategoryWidget(category: category),
  
                              ],
                            ),
                          ),
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 16.0),
                        child: Consumer&lt;AppState&gt;(
                          builder: (context, appState, _) =&gt; Column(
                            children: &lt;Widget&gt;[
                              for (final event in Event.where((e) =&gt; e
                                  .categoryIds
                                  .contains(appState.selectedCategoryId)))
                                GestureDetector(
                                  onTap: () {
                                    Navigator.of(context).push(
                                      MaterialPageRoute(
                                        builder: (context) =&gt;
                                            EventDetailsPage(event: event),
                                      ),
                                    );
                                  },
                                  child: EventWidget(
                                    event: event,
                                  ),
                                )
                            ],
                          ),
                        ),
                      ),
                      FloatingActionButton.extended(
                       onPressed: () {
                         Navigator.push(context,
                         MaterialPageRoute(builder: (context) =&gt; FormPage()));
                       },
                       label: Text('Create'),
                       icon: Icon(Icons.create),
                       elevation: 2,
                       shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(16.0))),
                       backgroundColor: Color(0xFF6F35A5),
                      ),
                      ElevatedButton(
                    child: Text('Sign Out',
                        style: TextStyle(
                          color: Colors.black,
                          fontSize: 16,
                        )),
                    onPressed: () {
                      context.read&lt;AuthenticationService&gt;().signOut();
                      Navigator.of(context).push(MaterialPageRoute(builder: (context) =&gt; LoginScreen()));
                    }),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      );
    }
  
  
  }
  
  class CircularButton extends StatelessWidget {
    final double width;
    final double height;
    final Color color;
    final Icon icon;
    final Function onClick;
  
    CircularButton(
        {this.color, this.width, this.height, this.icon, this.onClick});
  
    @override
    Widget build(BuildContext context) {
      return Container(
        decoration: BoxDecoration(color: color, shape: BoxShape.circle),
        width: width,
        height: height,
        child: IconButton(
          icon: icon,
          enableFeedback: true,
          onPressed: onClick,
        ),
      );
    }
  }
  </code></pre>
  <p>Specifically this part:</p>
  <pre class="lang-dart prettyprint-override"><code>Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 16.0),
                        child: Consumer&lt;AppState&gt;(
                          builder: (context, appState, _) =&gt; Column(
                            children: &lt;Widget&gt;[
                              for (final event in Event.where((e) =&gt; e
                                  .categoryIds
                                  .contains(appState.selectedCategoryId)))
                                GestureDetector(
                                  onTap: () {
                                    Navigator.of(context).push(
                                      MaterialPageRoute(
                                        builder: (context) =&gt;
                                            EventDetailsPage(event: event),
                                      ),
                                    );
                                  },
                                  child: EventWidget(
                                    event: event,
                                  ),
                                )
                            ],
                          ),
                        ),
                      ),
  </code></pre>