title: How to get data from Firebase Realtime Database into list in Flutter?
body: <p>I am trying to retrieve data from Firebase Realtime Database into a list in Flutter using a model. My list is returning as null when I do this. I have read several other posts about using Firebase with Flutter but have not found a clear answer. Here is my current approach (the object is called a 'need', and I am trying to retrieve and display a list of needs):</p>
  
  <p>Model:</p>
  
  <pre><code>import 'package:firebase_database/firebase_database.dart';
  
  class Need {
    final String id;
    final String imageUrl;
    final String caption;
    final String title;
  
    Need({
      this.id,
      this.imageUrl,
      this.caption,
      this.title,
    });
  
    Need.fromSnapshot(DataSnapshot snapshot) :
      id = snapshot.key,
      imageUrl = snapshot.value["imageUrl"],
      caption = snapshot.value["caption"],
      title = snapshot.value["postTitle"];
  
    toJson() {
      return {
        "imageUrl": imageUrl,
        "caption": caption,
        "title": title,
      };
    }
  }
  </code></pre>
  
  <p>Database service with Firebase query:</p>
  
  <pre><code>import 'package:firebase_database/firebase_database.dart';
  import 'package:Given_Flutter/models/need_model.dart';
  
  class DatabaseService {
  
    static Future&lt;List&lt;Need&gt;&gt; getNeeds() async {
      Query needsSnapshot = await FirebaseDatabase.instance
        .reference()
        .child("needs-posts")
        .orderByKey();
  
      print(needsSnapshot); // to debug and see if data is returned
  
      List&lt;Need&gt; needs;
  
      Map&lt;dynamic, dynamic&gt; values = needsSnapshot.data.value;
      values.forEach((key, values) {
        needs.add(values);
      });
  
      return needs;
    }
  }
  </code></pre>
  
  <p>ListView:</p>
  
  <pre><code>import 'package:flutter/material.dart';
  import 'package:Given_Flutter/models/need_model.dart';
  import 'package:Given_Flutter/services/database_service.dart';
  import 'package:Given_Flutter/need_view.dart';
  
  class Needs extends StatefulWidget {
    static final String id = 'needs_screen';
  
    @override
    _NeedsState createState() =&gt; _NeedsState();
  }
  
  class _NeedsState extends State&lt;Needs&gt; {
    List&lt;Need&gt; _needs = [];
  
    @override
    void initState() {
      super.initState();
      _setupNeeds();
    }
  
    _setupNeeds() async {
      List&lt;Need&gt; needs = await DatabaseService.getNeeds();
      setState(() {
        _needs = needs;
      });
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: RefreshIndicator(
          onRefresh: () =&gt; _setupNeeds(),
          child: ListView.builder(
            itemCount: _needs.length,
            itemBuilder: (BuildContext context, int index) {
              Need need = _needs[index];
              return NeedView(
                need: need,
              );
            }
          )
        )
      );
    }
  }
  </code></pre>
  
  <p>Firebase Realtime Database structure:</p>
  
  <p><a href="https://i.stack.imgur.com/KJP0g.png" rel="noreferrer">Firebase example</a></p>
  
  <p>What am I doing wrong?</p>