0

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 ?

0

1 Answer 1

3

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()!);
  }
Sign up to request clarification or add additional context in comments.

4 Comments

Worked! thanks , this also worked for me "file!.writeAsBytesSync(excel.encode()!)" . I want to know why these things happens even using the same code from documentation. These type, argument, parameter errors always comes to me ;/
documentation sample code was not updated maybe...
@MauriceRaguse I am getting an error at 'File(join(' It says that The function 'join' isn't defined.
how to add cellstyle for first row in this example

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.