addNewKeyValueToPlist function

Future<void> addNewKeyValueToPlist(
  1. String filePath,
  2. String key,
  3. String value
)

Info.plist

Implementation

Future<void> addNewKeyValueToPlist(
  String filePath,
  String key,
  String value,
) async {
  // 读取 plist 文件
  final file = File(filePath);
  final document = XmlDocument.parse(file.readAsStringSync());

  // 查找 <dict> 标签
  final dictElement = document.findAllElements('dict').first;

  // 创建新的 key-value 元素
  final newKey = XmlElement(XmlName('key'), [], [XmlText(key)]);
  final newValue = XmlElement(XmlName('string'), [], [XmlText(value)]);

  // 将新的 key-value 添加到 dict
  dictElement.children.add(newKey);
  dictElement.children.add(newValue);

  // 将修改后的内容写回文件
  file.writeAsStringSync(document.toXmlString(pretty: true));
}