Currently, I am accessing a specific JSON file country.json by DefaultAssetBundle as follows:
Future<List<Country>> getCountryFromJson(BuildContext context) async {
String jsonString =
await DefaultAssetBundle.of(context).loadString('assets/country.json');
List<dynamic> raw = jsonDecode(jsonString);
return raw.map((e) => Country.fromJson(e)).toList();
}
However I want to update new json file (same name) by uploading it to FirebaseStorage, then every time the app open, it will download new file via get http and access it later (the app still accesses the old file when the new file hasn't finished downloading)
So I want to ask how to do the following:
- Option 1: Download the new file country.json from Http link
and overwrite to the old file in assets
- Option 2: If it is unable to
interfere with the assets, then download it to wherever possible in local storage and
prioritize access to the new file over the old file in the assets
this is the main file:
import 'dart:convert';
import 'dart:core';
import 'package:ask/country.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Future<List<Country>> getCountryFromJson(BuildContext context) async {
String jsonString =
await DefaultAssetBundle.of(context).loadString('assets/country.json');
List<dynamic> raw = jsonDecode(jsonString);
return raw.map((e) => Country.fromJson(e)).toList();
}
Widget build(BuildContext context) {
return new MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('Country')),
body: Container(
child: FutureBuilder(
future: getCountryFromJson(context),
builder: (context, data) {
if (data.hasData) {
List<Country> countries = data.data;
return ListView.builder(
itemCount: countries.length,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.all(5.0),
child: Column(children: <Widget>[
Text(
countries[index].country,
)
]));
});
} else {
return Center(child: CircularProgressIndicator());
}
}))));
}
}