How to import file from local storage into DB in Flutter

2,433

Back to beginning and yet a little bit closer to the solution I need. I have file_picker (https://pub.dartlang.org/packages/file_picker), which helps to pick file using file explorer:

    String _filePath;

  void getFilePath() async {
    try {
      String filePath = await FilePicker.getFilePath(
          type: FileType.CUSTOM, fileExtension: 'csv');
      if (filePath == '') {
        return;
      }
      print("Path: " + filePath);
      setState(() {
        this._filePath = filePath;
      });
    } on PlatformException catch (e) {
      print("Error picking file: " + e.toString());
    }
  }

Using above code returns the path of the file e.g. "/storage/emulated/0/Download/1.csv".

Now I use this path to read the contents of the file:

    ...
    RaisedButton(
                    child: const Text('Import data - dummy'),
                    color: Theme.of(context).accentColor,
                    elevation: 4.0,
                    splashColor: Colors.blueGrey,
                    onPressed: () {
                      print('Verifying click done');
                      // Show contents of the file
                      readContents();
                    },
                  ),
                ],
              ),
            ),
            floatingActionButton: FloatingActionButton(
              onPressed: getFilePath,
              tooltip: 'Choose file to import',
              child: new Icon(Icons.sd_storage),
            )
    ...

Future<File> get _localFile async {
    final path = await _filePath;
    return File('$path');
  }

  Future<int> readContents() async {
    try {
      final file = await _localFile;

      // Read the file
      String contents = await file.readAsString();

      return int.parse(contents);
    } catch (e) {
      // If we encounter an error, return 0
      return 0;
    }
  }

Now as the above code should return contents of a CSV file, it does nothing. CSV file contains a list of items.

Can somebody let me know why and be so kind to show me how I can save parsed content of a file to a string or even better strings which represent each column from the file?

THANKS!

Share:
2,433
jurijk
Author by

jurijk

Updated on December 08, 2022

Comments

  • jurijk
    jurijk over 1 year

    My goal is to open a CSV file from phone local storage ~/Downloads and import it into the DB.

    I'm using Flutter and to this day I have looked over a dozen examples without a success.

    However I was able to get contents of the file printed in the console, but for that I had to have a file in assets/res folder in the app project.

    If I use getExternalStorageDirectory, I get emulated/storage/0/ as a result. How to get to device's internal storage (not external storage as phone used will not have an option for a SD card or if they will, it will not be used)?

    For the DB, I was able to get a working example using sqflite, but I as I'm total noob, I can't get it to work within my already designed app.

  • jurijk
    jurijk over 5 years
    Now I'm another step closer to getting the result I need.So path to the file is no longer an issue, but what I need to do next is to get the contents of tha file saved into an array so that I can later import this to the DB. Can someone please point me to the simplest way of achieving that? Please keep in mind I just started with this recently and done programming quite a long time ago. THANKS!