title: In Flutter save firebase real time data in local storage json and populate list view from json
body: <p>In flutter, I want to get data from firebase realtime database and save it in local storage as json and populate list view from that json. If database updated in firebase ,then Json should get update and should reflect in list view.</p>
  
  <pre><code>DataSnapshot firebaseData;
  File allnews_json;
  Directory dir;
  String allnews_filename = "allnews.json";
  bool fileExists = false;
  
  @override
    void initState() {
      super.initState();
       getApplicationDocumentsDirectory().then((Directory directory) {
            dir = directory;
            allnews_json = new File(dir.path + "/" + allnews_filename);
            fileExists = allnews_json.existsSync();
            if (fileExists) this.setState(() =&gt; fileContent = JSON.decode(allnews_json.readAsStringSync()));
          });
  
  
       FirebaseDatabase.instance.reference().child('news')
         .once()
         .then((onValue) {
            firebaseData = onValue.value;
            write(firebaseData);
         });
  }
  
  void write(DataSnapshot snapshot) async {
  
      print("Creating file!");
      File file = new File(dir.path + "/" + allnews_filename);
      file.createSync();
  
      final String key = snapshot.key;
      final String headline = snapshot.value['title'];
      final int news = snapshot.value['content'];
      String content = '{"$key":{"headline":"$headline","news":"$news"}}';
      file.writeAsStringSync(content);
    }
  </code></pre>
  
  <p>This is the code i have so far. But there is something wrong in this too.</p>