flutter 没有为类型“CsvController”定义方法“getExternalStorageDirectory”

zf2sa74q  于 2023-01-27  发布在  Flutter
关注(0)|答案(2)|浏览(159)

我正在尝试创建CSV Controller,但出现语法错误The method 'getExternalStorageDirectory' isn't defined for the type 'CsvController'
这是我有问题的部分:

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;
  }

这里还有一个错误Undefined name 'General',这是下面的部分相关:

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;
    }
  }

这是下面的完整代码我有:

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;
    }
  }
}

编辑次数
解决了这个问题后,我仍然对通用关键字有疑问。

sbdsn5lh

sbdsn5lh1#

我已经发现了问题,因为我没有定义path provider包:
这是我的完整代码

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;
    }
  }
}
e4yzc0pl

e4yzc0pl2#

发生这种情况是因为你没有一个路径提供者的导入,最糟糕的是它没有显示建议导入。
在类顶部添加以下行并将修复
导入“包:路径_提供者/路径_提供者. dart”;

相关问题