Reputation: 11
I'm trying to load a json file and track the changes. but after the first load it never loads it again.
jsondata = await rootBundle.rootBundle.loadString('assets/data.json');
it seems that it loads it once and keeps that in memory and when it runs through this line again it doesn't load it again so I can't see the json's changes.
here is the json loading and classifying:
class JsonData {
JsonData({
required this.PostUpSale,
required this.WaitingForSpecialDelivery,
required this.SentOrders1,
required this.OutForSpecialDelivery,
required this.StuckOrders,
required this.Shulam,
required this.Name1,
required this.WaitingOrders1,
required this.Display1,
required this.Name2,
required this.WaitingOrders2,
required this.SentOrders2,
required this.Display2,
required this.Name3,
required this.WaitingOrders3,
required this.SentOrders3,
required this.Display3,
required this.Name4,
required this.WaitingOrders4,
required this.SentOrders4,
required this.Display4,
});
late final int PostUpSale;
late final int WaitingForSpecialDelivery;
late final int SentOrders1;
late final int OutForSpecialDelivery;
late final int StuckOrders;
late final int Shulam;
late final String Name1;
late final int WaitingOrders1;
late final bool Display1;
late final String Name2;
late final int WaitingOrders2;
late final int SentOrders2;
late final bool Display2;
late final String Name3;
late final int WaitingOrders3;
late final int SentOrders3;
late final bool Display3;
late final String Name4;
late final int WaitingOrders4;
late final int SentOrders4;
late final bool Display4;
JsonData.fromJson(Map<String, dynamic> json) {
PostUpSale = json['PostUpSale'];
WaitingForSpecialDelivery = json['WaitingForSpecialDelivery'];
SentOrders1 = json['SentOrders1'];
OutForSpecialDelivery = json['OutForSpecialDelivery'];
StuckOrders = json['StuckOrders'];
Shulam = json['Shulam'];
Name1 = json['Name1'];
WaitingOrders1 = json['WaitingOrders1'];
Display1 = json['Display1'];
Name2 = json['Name2'];
WaitingOrders2 = json['WaitingOrders2'];
SentOrders2 = json['SentOrders2'];
Display2 = json['Display2'];
Name3 = json['Name3'];
WaitingOrders3 = json['WaitingOrders3'];
SentOrders3 = json['SentOrders3'];
Display3 = json['Display3'];
Name4 = json['Name4'];
WaitingOrders4 = json['WaitingOrders4'];
SentOrders4 = json['SentOrders4'];
Display4 = json['Display4'];
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['PostUpSale'] = PostUpSale;
_data['WaitingForSpecialDelivery'] = WaitingForSpecialDelivery;
_data['SentOrders1'] = SentOrders1;
_data['OutForSpecialDelivery'] = OutForSpecialDelivery;
_data['StuckOrders'] = StuckOrders;
_data['Shulam'] = Shulam;
_data['Name1'] = Name1;
_data['WaitingOrders1'] = WaitingOrders1;
_data['Display1'] = Display1;
_data['Name2'] = Name2;
_data['WaitingOrders2'] = WaitingOrders2;
_data['SentOrders2'] = SentOrders2;
_data['Display2'] = Display2;
_data['Name3'] = Name3;
_data['WaitingOrders3'] = WaitingOrders3;
_data['Display3'] = Display3;
_data['Name4'] = Name4;
_data['WaitingOrders4'] = WaitingOrders4;
_data['SentOrders4'] = SentOrders4;
_data['Display4'] = Display4;
return _data;
}
}
void jorgeDeJson() async {
var jsondata = '0';
jsondata = await rootBundle.rootBundle.loadString('assets/data.json');
JsonData userDetails = JsonData.fromJson(jsonDecode(jsondata));
Shulam = userDetails.Shulam;
PostUpSale = userDetails.PostUpSale;
WaitingForSpecialDelivery = userDetails.WaitingForSpecialDelivery;
StuckOrders = userDetails.StuckOrders;
OutForSpecialDelivery = userDetails.OutForSpecialDelivery;
Name1 = userDetails.Name1;
WaitingOrders1 = userDetails.WaitingOrders1;
SentOrders1 = userDetails.SentOrders1;
Name2 = userDetails.Name2;
WaitingOrders2 = userDetails.WaitingOrders2;
SentOrders2 = userDetails.SentOrders2;
Name3 = userDetails.Name3;
WaitingOrders3 = userDetails.WaitingOrders3;
SentOrders3 = userDetails.SentOrders3;
Name4 = userDetails.Name4;
WaitingOrders4 = userDetails.WaitingOrders4;
SentOrders4 = userDetails.SentOrders4;
TotalSent = SentOrders1 + SentOrders2 + SentOrders3 + SentOrders4;
print(TotalSent);
print(Shulam);
}
here is the main loop:
Future<void> main() async {
while (1 == 1) {
print('Before RunUp');
runApp(MyApp());
print('Before Wait');
await Future.delayed(Duration(seconds: 5));
print('After Wait');
jorgeDeJson();
print('After Jorge');
}
}
many thanks!!!!
Upvotes: 1
Views: 109
Reputation: 660
not sure your process.
//try to await for the json to be loaded
Future<void> main() async {
while (1 == 1) {
print('Before RunUp');
runApp(MyApp());
print('Before Wait');
await Future.delayed(Duration(seconds: 5));
print('After Wait');
await jorgeDeJson(); //<-- here
print('After Jorge');
}
}
Couple of things that are going to ease posible future developments.
you are creating an endless loop, avoid it, instead react to user changes when user changes something.
Future<void> main() async {
runApp(MyApp());
}
MyApp should have the logic to load and refresh when user changes some value previouly loaded from file.
Try to encapsulate concrete functionality, this will allow you to have more granular control
// function to load and return any json data in project bundle
Future<Map<String, dynamic>> parseJsonFromAssets(String assetsPath) async {
return json.decode(await rootBundle.loadString(assetsPath));
}
Future<void> jorgeDeJson() async {
JsonData userDetails = JsonData.fromJson(await parseJsonFromAssets('assets/data.json'));
//..do stuff
}
Another recomendation and if you are the system owner of generated json, try to avoid harcoding, Name1, Name2. Normalize your data.
Meaning:
late final String Name1;
late final int WaitingOrders1;
late final bool Display1;
late final String Name2;
late final int WaitingOrders2;
late final int SentOrders2;
late final bool Display2;
late final String Name3;
late final int WaitingOrders3;
late final int SentOrders3;
late final bool Display3;
late final String Name4;
late final int WaitingOrders4;
late final int SentOrders4;
late final bool Display4;
Reestructuring your your Model and jsonData, will allow you to iterate regardless of waiting orders you might have in the future
List<WaitingOrder> waitingOrders = [
WaitingOrder(
Name: 'name value 1',
Display: 'display value 1'
),
WaitingOrder(
Name: 'name value 2',
Display: 'display value 2'
),
]
Upvotes: 1