this method running always. I checked with API. If API value change I delete my database and insert again. Is that right way to use like this scenario? (can use streamWidget or FutureWidget? If can How? )
Error message:
[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: RangeError (index): Invalid value: Valid value range is empty: 0
checkQuick(String url, String token) async {
result =
(await HelperDatabase1().displayGetUserPreference()).elementAt(0)?.data;
final response = await http.get(
'$url/nativeapi/v1.0/User/GetUserPreference',
headers: {'Authorization': 'Bearer $token'},
);
final jsonResponse = json.decode(response.body);
GetUserPreference model = GetUserPreference.fromJson(jsonResponse);
var data = GetUserPreference(data: model.data);
if (result != data.data) {
await HelperDatabase1().deleteGetUserPreference();
await HelperDatabase1().storeGetUserPreference(url, token);
}
}
The error is because displayGetUserPreference returns an empty list, so in order to fix your issue you should check if the list is not empty before get the item.
final list = (await HelperDatabase1().displayGetUserPreference());
if (list.isNotEmpty)
result = list.elementAt(0).data;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With