addLineToInfoPlist static method

void addLineToInfoPlist({
  1. required String key,
  2. String? value,
  3. bool autoClose = false,
})

Implementation

static void addLineToInfoPlist(
    {required String key, String? value, bool autoClose = false}) {
  final file = File('ios/Runner/Info.plist');
  final xml = XmlDocument.parse(file.readAsStringSync());
  final root = xml.rootElement;
  final dict = root.findElements("dict").first;
  final elementKey = XmlElement(XmlName('key'), [], [XmlText(key)]);
  final elementValue = autoClose
      ? XmlElement(XmlName('false'), [], [])
      : XmlElement(XmlName('string'), [], [XmlText(value!)]);

  int position = dict.children.indexOf(dict.findElements("key").firstWhere(
      (element) => element.innerText == "UILaunchStoryboardName"));

  dict.children.insert(position, elementValue);
  dict.children.insert(position, elementKey);
  final prettyXml = xml.toXmlString(pretty: true);
  file.writeAsStringSync(prettyXml);
}