美文网首页
flutter复制assets工程文件到沙盒中

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

作者: smallLabel | 来源:发表于2021-07-14 00:07 被阅读0次
// 拷贝demo到沙盒
Future<void> copyDemoToSandBox() async {
  // 获取cache目录,
  Directory cache = await getTemporaryDirectory();
  final manifestContent = await rootBundle.loadString('AssetManifest.json');
  final Map<String, dynamic> 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<int> 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工程文件到沙盒中

      本文链接:https://www.haomeiwen.com/subject/seuepltx.html