flutter复制assets工程文件到沙盒中

// 拷贝demo到沙盒
Future copyDemoToSandBox() async {
  // 获取cache目录,
  Directory cache = await getTemporaryDirectory();
  final manifestContent = await rootBundle.loadString('AssetManifest.json');
  final Map manifestMap = convert.json.decode(manifestContent);

// 这里自己过滤需要复制的文件夹
  manifestMap.keys
      .where((key) => key.contains('demo/') && !key.contains('.DS_'))
      .forEach((element) async {
    // 读取数据
    ByteData data = await rootBundle.load(element);
    List bytes =
        data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);

    String dataPath = path.join(cache.path, element);
    File file = File(dataPath);
    await file.create(recursive: true);
    await File(dataPath).writeAsBytes(bytes);
  });
}
复制前工程目录结构

复制后沙盒目录文件

你可能感兴趣的:(flutter复制assets工程文件到沙盒中)