Home > database >  How to save the excel file using excel dart package?
How to save the excel file using excel dart package?

Time:02-10

I'm trying to implement a function which could select an excel file from phone's storage, edit some values and then finally save it using excel dart package. My function :

Future<void> createExcel() async {
   File file = await selectFile() //this is working fine
   
   //setting some configurations for excel
    var bytes = file!.readAsBytesSync();
    var excel = Excel.decodeBytes(bytes);
    Sheet sheetObject = excel['Sheet1'];
    
   //Updating excel values logic
   for(int i = 1; i<1000;   i){
     var cell = sheetObject.cell(CellIndex.indexByString("A$i"));
     cell.value = 8; // dynamic values support provided;
   }

    //saving excel
    excel.encode()!.then((onValue) {   //this line is giving error: "The method 'then' isn't defined for the type 'List'. "
        File(join(file!.path))
        ..createSync(recursive: true)
        ..writeAsBytesSync(onValue);
    });

}

I couldn't be able to use .then() with excel.encode() Is there any way to solve this ?

CodePudding user response:

sounds like... your encode method returns a LIST and not a Future<List>... so put your excel.encode direct in your writeAsBytesSync method

like this:

     Future<void> createExcel() async {
   File file = await selectFile() //this is working fine
   
   //setting some configurations for excel
    var bytes = file!.readAsBytesSync();
    var excel = Excel.decodeBytes(bytes);
    Sheet sheetObject = excel['Sheet1'];
    
   //Updating excel values logic
   for(int i = 1; i<1000;   i){
     var cell = sheetObject.cell(CellIndex.indexByString("A$i"));
     cell.value = 8; // dynamic values support provided;
   }

    //saving excel
    File(join(file!.path))
    ..createSync(recursive: true)
    ..writeAsBytesSync(excel.encode()!);
  }
  • Related