The method 'getExternalStorageDirectory' isn't defined for the type 'CsvController'

Viewed 443

I am trying to create a CSV Controller but I got a syntax error The method 'getExternalStorageDirectory' isn't defined for the type 'CsvController'

Here's the part I have problem in:

  static Future<String> _getFilePath(String fileName) async {
    Io.Directory appDocumentsDirectory = await getExternalStorageDirectory(); // 1
    String appDocumentsPath = appDocumentsDirectory.path; // 2
    String filePath = '$appDocumentsPath/$fileName.csv'; // 3
    return filePath;
  }

Here's also I have an error Undefined name 'General' and this is the below part related:

 static Future<Io.File> _saveFile(String fileDataString, {index = 0}) async {
    try {
      Io.File file = Io.File(await _getFilePath(
          "${General.getDate().millisecondsSinceEpoch}" +
              (index > 0 ? "($index)" : "")));
      if (!file.existsSync()) {
        // 1
        file.writeAsStringSync(fileDataString); // 2
        return file;
      } else {
        return _saveFile(fileDataString, index: index + 1);
      }
    } catch (e) {
      print(e.toString());
      return null;
    }
  }

and this is the below full code I have:

import 'package:csv/csv.dart';
import 'dart:io' as Io;
import 'package:ext_storage/ext_storage.dart';

class CsvController {

  static Future<Io.File> getCsvFromList(List<List<dynamic>> csvDataList) async {
    try {
      String csvDataString = const ListToCsvConverter().convert(csvDataList);
      Io.File csvFile = await _saveFile(csvDataString);
      return csvFile;
    } catch (e) {
      print(e.toString());
      return null;
    }
  }

  static Future<Io.File> getCsvFromString(String csvString) async {
    try {
      Io.File csvFile = await _saveFile(csvString);
      return csvFile;
    } catch (e) {
      print(e.toString());
      return null;
    }
  }

  static Future<String> _getFilePath(String fileName) async {
    Io.Directory appDocumentsDirectory = await getExternalStorageDirectory(); // 1
    String appDocumentsPath = appDocumentsDirectory.path; // 2
    String filePath = '$appDocumentsPath/$fileName.csv'; // 3
    return filePath;
  }

  static Future<Io.File> _saveFile(String fileDataString, {index = 0}) async {
    try {
      Io.File file = Io.File(await _getFilePath(
          "${General.getDate().millisecondsSinceEpoch}" +
              (index > 0 ? "($index)" : "")));
      if (!file.existsSync()) {
        // 1
        file.writeAsStringSync(fileDataString); // 2
        return file;
      } else {
        return _saveFile(fileDataString, index: index + 1);
      }
    } catch (e) {
      print(e.toString());
      return null;
    }
  }
}

#Edited

After solved it, I have still have a problem with the General Key word.

1 Answers

I have figured the problem, because I didn't defined the path provider package:

and this is the full code I have:

import 'package:csv/csv.dart';
import 'dart:io' as Io;
import 'package:path_provider/path_provider.dart';

class CsvController {

  static Future<Io.File> getCsvFromList(List<List<dynamic>> csvDataList) async {
    try {
      String csvDataString = const ListToCsvConverter().convert(csvDataList);
      Io.File csvFile = await _saveFile(csvDataString);
      return csvFile;
    } catch (e) {
      print(e.toString());
      return null;
    }
  }

  static Future<Io.File> getCsvFromString(String csvString) async {
    try {
      Io.File csvFile = await _saveFile(csvString);
      return csvFile;
    } catch (e) {
      print(e.toString());
      return null;
    }
  }

  static Future<String> _getFilePath(String fileName) async {
    Io.Directory appDocumentsDirectory = await getExternalStorageDirectory(); // 1
    String appDocumentsPath = appDocumentsDirectory.path; // 2
    String filePath = '$appDocumentsPath/$fileName.csv'; // 3
    return filePath;
  }

  static Future<Io.File> _saveFile(String fileDataString, {index = 0}) async {
    try {
      Io.File file = Io.File(await _getFilePath(
          "${General.getDate().millisecondsSinceEpoch}" +
              (index > 0 ? "($index)" : "")));
      if (!file.existsSync()) {
        // 1
        file.writeAsStringSync(fileDataString); // 2
        return file;
      } else {
        return _saveFile(fileDataString, index: index + 1);
      }
    } catch (e) {
      print(e.toString());
      return null;
    }
  }
}
Related