I am currently displaying the data by calling the JSON file from Firebase Storage, but I want that instead of download JSON file every single time to show data => I will check if the JSON file from the Firebase Store has changed:
- If it changed => download the new JSON file to Local directory and display it.
- Otherwise => display the old JSON file in Local directory (This old JSON file will be downloaded when first time App open)
About JSON File
This is JSON link after I upload JSON to Firebase Storage:
As far as I know, this link is made up of 2 parts:
First part: https://firebasestorage.googleapis.com/v0/b/tft-test-48c87.appspot.com/o/loadData.json
Last part: ?alt=media&token=
+ 2e3d416-62dc-4137-93a3-59ade95ac38f
(it is value of String: "downloadTokens" in First part)
In the First part of the link, there is all information about JSON file, and especially I think that value of String "updated" can be used as a condition for the purpose of downloading files or not.
Ex. "updated": "2020-08-04T14:30:10.920Z",
The value of this String updated will change every time I upload a new JSON file with the same name as the old JSON file but the link download will not change.
Steps
So I want to do the following:
- Create file to store String "updated" in Local directory (Ex. "updated": null) and where to store the JSON file after download to Local directory
- Open App
- Check String "updated" in link First Part:
Case A: if value of String "updated" in First Part
!=
value of String "updated" in Local directory =>- Step 1: download JSON file (by link:
First part
+?alt=media&token=
+downloadTokens
) to Local directory (If the old json file already exists, it will be replaced) - Step 2: overwrite value of String "updated" in Local directory by value of String "updated" in Firebase Storage
- Step 3: access JSON file in Local directory to display data
- Step 1: download JSON file (by link:
Case B: if value of String "updated" in First Part
==
value of String "updated" in Local directory => do nothing, just access JSON file in Local directory to display data
I know this is a lot of questions for one post, I'm a newbie with code and if I split it up into a few posts then it is very difficult to combine them for me. So I hope the answer with full code, that would be great. Thanks. This is the main file:
import 'package:ask/model/load_data_model.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class LoadDataPage extends StatefulWidget {
@override
_LoadDataPageState createState() => _LoadDataPageState();
}
class DataServices {
static const String url = 'https://firebasestorage.googleapis.com/v0/b/tft-test-48c87.appspot.com/o/loadData.json?alt=media&token=92e3d416-62dc-4137-93a3-59ade95ac38f';
static Future<List<Data>> getData() async {
try {
final response = await http.get(url);
if (200 == response.statusCode) {
final List<Data> data = dataFromJson(response.body);
return data;
} else {
return List<Data>();
}
} catch (e) {
return List<Data>();
}
}
}
class _LoadDataPageState extends State<LoadDataPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Load Data')),
body: FutureBuilder(
future: DataServices.getData(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
List<Widget> children;
List<Data> _data = snapshot.data;
if (snapshot.hasData) {
return ListView.builder(
itemCount: _data.length,
itemBuilder: (context, index) {
return Column(
children: [Text(_data[index].data)],
);
},
);
} else {
children = <Widget>[SizedBox(child: CircularProgressIndicator(), width: 60, height: 60), const Padding(padding: EdgeInsets.only(top: 16), child: Text('Loading...'))];
}
return Center(child: Column(mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: children));
}));
}
}
Another Steps
EdwynZN's answer worked great for me, however, I edit the post to add one more case which I think will make load page ASAP, So please help me again:
After open Page => readFile
> compareLastUpdate
> _lastUpdateDB
& _createFile
- Case A: The first time the app opens =>
readFile
: false >_lastUpdateDB
&_createFile
>readFile
again - Case B: Not the first time the app opens:
- the data is still loaded immediately from the old JSON, at the same time, run in background:
compareLastUpdate
:- If update times are the same => do nothing
- If update times are different =>
_lastUpdateDB
&_createFile
- the data is still loaded immediately from the old JSON, at the same time, run in background:
P/S: With this flow, the second time they open the page then new data will be displayed, right? But I wonder that if using StatefulWidget
=> after the new JSON file is overwritten to the old JSON file => will the phone screen display new data after that?