title: How to convert byte array to .csv file in flutter
body: <p>How i can convert byte array(send as a string in JSON) to .csv file.</p>
  <p>What I have done so far is</p>
  <p>Converted the JSON receive as a sting to unit8list</p>
  <p>and created a new file with .csv file and writing content as <code>file.writeAsBytes(bytes)</code></p>
  <p>I am getting .csv file but the data is written in single-cell(is as string received in received JSON response )</p>
  <pre><code>// response from json
  DownloadResponse  value = DownloadResponse.fromJson(jsonDecode(response.body));
  
  List&lt;int&gt; bytes = utf8.encode(value.object);
     Uint8List bytes8 = Uint8List.fromList(bytes);
     print(&quot;-------------- List&lt;int&gt; ------------&quot;);
  
     print(bytes);
     print(&quot;-------------- END List&lt;int&gt; ------------&quot;);
  
     print(&quot;\n\n-------------- List&lt;Unit8&gt; ------------&quot;);
  
     print(bytes8);
  
     print(&quot;\n\n-------------- END List&lt;Unit8&gt; ------------&quot;);
  
  print(&quot;location supply!!&quot;);
     File file = new File('/storage/emulated/0/Download/${currentFile.fileName}');
     File filety = new File('/storage/emulated/0/Download/file2.csv');
  
  
     print(&quot;creating file&quot;);
     var file2 = await file.writeAsBytes(bytes);
     var file3 = await filety.writeAsBytes(bytes8);
  
     var parent = file2.parent;
     var parent2 = file2.parent;
     print(&quot;parant: $parent&quot;);
     print(&quot;parant2: $parent2&quot;);
  
  
  </code></pre>
  <p><strong>main.dart</strong></p>
  <pre><code>import 'dart:async';
  import 'dart:convert';
  import 'dart:io';
  import 'dart:typed_data';
  import 'package:ext_storage/ext_storage.dart';
  import 'package:flutter/material.dart';
  import 'package:path_provider/path_provider.dart';
  import 'package:path/path.dart' as path;
  import 'package:permission_handler/permission_handler.dart';
  
  void main() {
   runApp(
     MaterialApp(
       title: 'Reading and Writing Files',
       home: MyApp(),
     ),
   );
  }
  
  class MyApp extends StatefulWidget {
   @override
   _AppState createState() =&gt; _AppState();
  }
  
  class _AppState extends State&lt;MyApp&gt; {
   String data;
   String byteStr = &quot;MWNvbnRhaW5lcl9udW1iZXIsY2F0ZWdvcnksZW50cnlfbW9kZSxle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I3Mjk0NDgsRSwsLCwsLCwsLCxNU0MsMjEwMjAxLCwsLCxTaGlwcGluZyBsaW5lIGNvZGUgZG9lcyBub3QgZXhpc3RzClRDTFUyNzI5NDQ4LEUsUiwsMDMwMjIwMjEgMTU6NTQ6MDAsLDIwLE4sLCxJQ0RUVlQtVCxNU0MsMjEwMjAxLCwsTixOLE5leHQgZGVzdGluYXRpb24gZG9lcyBub3QgZXhpc3RzClRDTFU4NDM1MTE2LEksLCwsLCwsLCwsTVNLLDIxMDEwNiwsLCwsU2hpcHBpbmcgbGluZSBjb2RlIGRvZXMgbm90IGV4aXN0cwpUQ05VMTQ5NDE4MixJLCwsLCwsLCwsLE1TSywyMTAxMDYsLCwsLFNoaXBwaW5nIGxpbmUgY29kZSBkb2VzIG5vdCBleGlzdHMKVENOVTE0OTQxODIsSSxWLFIsMzEwMTIwMjEgMjE6NTY6MjYsMDMwMjIwMjEgMTU6NTM6MjAsNDAsTiwsMzEwMTIwMjEgMjE6NTY6MjYsSUNESUxQLVQsTVNLLDIxMDEwNiwsLE4sWSxOZXh0IGRlc3RpbmF0aW9uIGRvZXMgbm90IGV4aXN0cwpUR0JVMzIxODc3NCxFLCwsLCwsLCwsLE1TQywyMTAyMDEsLCwsLFNoaXBwaW5nIGxpbmUgY29kZSBkb2VzIG5vdCBleGlzdHMKVEdCVTMyMTg3NzQsRSxSLCwwMzAyMjAyMSAxNjoxMjowMCwsMjAsTiwsLE9URkFDLE1TQywyMTAyMDEsLCxOLE4sTmV4dCBkZXN0aW5hdGlvbiBkb2VzIG5vdCBleGlzdHMK&quot;;
  
   Future&lt;File&gt; moveFile(File sourceFile, String newPath) async {
     try {
       /// prefer using rename as it is probably faster
       /// if same directory path
       return await sourceFile.rename(newPath);
     } catch (e) {
       /// if rename fails, copy the source file
       final newFile = await sourceFile.copy(newPath);
       return newFile;
     }
   }
  
   Future&lt;String&gt; get _localPath async {
     final directory = await getApplicationDocumentsDirectory();
     print(directory.path);
     return directory.path;
   }
  
   Future&lt;String&gt; dirpath() async {
     var path = await ExtStorage.getExternalStoragePublicDirectory(ExtStorage.DIRECTORY_DOWNLOADS);
     print(&quot;download: &quot;+path);
     return path;
   }
  
   Future&lt;File&gt; get _localFile async {
     final path = await _localPath;
      final extpath = await dirpath();
  
     File src = File('$path/counter.txt');
     String desc = '$extpath/counter.txt';
  
     var status = await Permission.storage.status;
     if (!status.isGranted) {
       await Permission.storage.request();
  
       var file =  await moveFile(src,desc);
       print(&quot;file::: ${file.path}&quot;);
     }
  
     // Permission.storage.request();
  
     File('$extpath/counter.txt');
  
     return File('$path/counter.txt');
   }
  
   Future&lt;String&gt; readContent() async {
     try {
       final file = await _localFile;
       // Read the file
       String contents = await file.readAsString();
       // Returning the contents of the file
       return contents;
     } catch (e) {
       // If encountering an error, return
       return 'Error!';
     }
   }
  
   Future&lt;File&gt; writeContent() async {
     final file = await _localFile;
     // Write the file
     return file.writeAsString('Hello Folk');
   }
  
   @override
   void initState() {
     super.initState();
      askedPermission();
   }
  
   @override
   Widget build(BuildContext context) {
     return Scaffold(
       appBar: AppBar(title: Text('Reading and Writing data')),
       body: Center(
  
         child: Column(
           children: [
             Text(
               'Data read from a file: \n $data',
             ),
             SizedBox(height: 20,),
             FlatButton(onPressed: (){
               setState(()  {
  
                  // write();
                 writeCsv();
  
               });
             }, child: Text(&quot;write&quot;)),
  
           ],
         ))
     );
   }
  
   Future&lt;void&gt; askedPermission() async {
     var status = await Permission.storage.status;
     print(&quot;start Status $status&quot;);
     if (!status.isGranted) {
       await Permission.storage.request();
       status = await Permission.storage.status;
  
         if(status.isGranted){
  
           print(&quot;location supply!!&quot;);
           File file = new File('/storage/emulated/0/Download/file.text');
           print(&quot;creating file&quot;);
           var file2 = await file.writeAsString(&quot;dhddhhd&quot;);
  
           var parent = file2.parent;
           print(&quot;parant: $parent&quot;);
  
          /* writeContent();
           readContent().then((String value) {
             setState(() {
               data = value;
             });
           });*/
         }
         else{
           print(&quot;No Permission to write File !!&quot;);
         }
  
     }
     print(&quot;end Status $status&quot;);
   }
  
   Future&lt;void&gt; write() async {
  
     print(&quot;location supply!!&quot;);
     File file = new File('/storage/emulated/0/Download/file.text');
     print(&quot;creating file&quot;);
     var file2 = await file.writeAsString(&quot;dhddhhd&quot;);
  
     var parent = file2.parent;
     print(&quot;parant: $parent&quot;);
   }
  
   Future&lt;void&gt; writeCsv() async {
  
     List&lt;int&gt; bytes = utf8.encode(byteStr);
     Uint8List bytes8 = Uint8List.fromList(bytes);
     print(&quot;-------------- List&lt;int&gt; ------------&quot;);
  
     print(bytes);
     print(&quot;-------------- END List&lt;int&gt; ------------&quot;);
  
     print(&quot;\n\n-------------- List&lt;Unit8&gt; ------------&quot;);
  
     print(bytes8);
  
     print(&quot;\n\n-------------- END List&lt;Unit8&gt; ------------&quot;);
  
     print(&quot;location supply!!&quot;);
     File file = new File('/storage/emulated/0/Download/file.csv');
     File filety = new File('/storage/emulated/0/Download/file2.csv');
  
     print(&quot;creating file&quot;);
     var file2 = await file.writeAsBytes(bytes);
     var file3 = await filety.writeAsBytes(bytes8);
  
     var parent = file2.parent;
     print(&quot;parant: $parent&quot;);
   }
  }
  
  </code></pre>