'Why data variables are persisting the old values in Flutter app?
I am so confused about the data persistence in my app. I am using google drive api to fetch a spreadsheet file(.xlsx) and then read and show the excel cell values in my app using an itemBuilder. The main problem is that when I edit some cells in my google spreadsheet on the browser and fetch the file(which is now updated) from the app, it does not show the new values. I noticed that my app takes some time to fetch file for the first time but after that it fetches immediately. I am going to show about how I am fetching file and updating it.
Initialization of variables:
//I am using excel dart package
late var excel;
late drive.DriveApi driveApi;
late Sheet sheet;
String fileId = "19jF3lOVW563LU6mEjqAjXVLNQ7poXY1Z"; //xlsx file id
Fetching of file from drive:
drive.Media? response = (await driveApi.files.get(fileId,downloadOptions: drive.DownloadOptions.fullMedia)) as drive.Media?;
List<int> dataStore = [];
response!.stream.listen((data) {
dataStore.insertAll(dataStore.length, data);
}, onDone: (){
excel = Excel.decodeBytes(dataStore); //excel variable declaration
sheet = excel['Sheet1'];
}, onError: (error) {
Fluttertoast.showToast(msg: error.toString());
});
Showing values of excel using itemBuilder
Widgets----
............
............
............
............
itemCount: sheet.maxRows - 1,
itemBuilder: (context, index) {
var cell = sheet.cell(CellIndex.indexByColumnRow( //taking values from excel sheets
rowIndex: index + 1, columnIndex: itr));
String val = cell.value.toString().toLowerCase();
if (cell.value == null ||
(val != 'present' && val != 'absent')) {
cell.value = 'absent';
cell.cellStyle = absent;
}
return ListTile(
subtitle: Text(cell.value.toString()),
title: Row(
children: <Widget>[
const Icon(Icons.person),
Expanded(
child: Text(sheet.rows[index + 1][0]!.value
.toString()),
............
............
............
............
Widgets----
Updating the excel file to google drive:
//First I am creating a unique pathName, then creating a file and saving the new excel values
//to the newly created file
final String path = (await getApplicationSupportDirectory()).path;
final String fileName = path + cryptoRandom() + '/$fileId.xlsx';
File newFile = File(fileName);
var driveFile = new drive.File();
driveFile.name = basename(newFile.path);
//Saving excel to newFile
File(join(newFile.path))
..createSync(recursive: true)
..writeAsBytesSync(excel.encode()!);
//Updating the newFile
await driveApi.files.update(driveFile, fileId,
uploadMedia: drive.Media(newFile.openRead(), newFile.lengthSync()));
//Deleting the created file
newFile.deleteSync(recursive: true);
Why the old values persist in my app, even when I am creating new file everytime and deleting them also. Why this is happening, I sthis because of some cache data or what ? Thanks
Solution 1:[1]
you can set as null or any default value. when first run page vars will be default one. and another place you can get your main datas.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | do?an k?sa |
