Chrome 打包的应用程序:从命令行 (LaunchData) 参数检索 Dart 中的 FileEntry

Chrome Packaged App: Retrieving FileEntry in Dart from command line (LaunchData) parameter

与关于此主题的另一个 post 类似,我一直在尝试使用本地 XML 文件的文件路径从命令行 运行 我的应用程序,以便我的main.dart 可以解析这个文件,以便从中提取与程序运行相关的信息。我对如何正确访问 launchData 中包含的 FileEntry 引用感到困惑 - onLaunched 事件的参数。

这是我目前拥有的:

manifest.json:

...
"permissions": [
    "storage",
    "fileSystem",
    "*://*/*"
],

"file_handlers" : {
    "any" : {
        "types" : [ "*" ]
    }
},
...

background.js:

chrome.app.runtime.onLaunched.addListener(function(launchData) {

  chrome.app.window.create(

  'htmlFile.html',

  {...},

  function(createdWindow) {
    createdWindow.contentWindow.launchData = launchData;
  });
});

此时,我无法从 main.dart 访问 launchData,因为我正在尝试

  FileEntry entry =  (chrome.app.window.current().contentWindow.launchData as chrome.LaunchData).items.elementAt(0).entry;

获取 FileEntry 导致访问 launchData 出错。结果,我真的很困惑我应该如何从我的 Dart 代码中访问我想要的 FileEntry

我最终用这个作为我的解决方案:

manifest.json:

...
"file_handlers": {
  "any": {
    "extensions": [
      "xml"
    ]
  }
}, 
...

background.js:

chrome.app.runtime.onLaunched.addListener(function(launchData) {

  chrome.app.window.create(
    'htmlSource.html',

  {
    id: 'mainWindow',
    state: "fullscreen"
  },

  function(createdWindow) {

    if(launchData.items !== undefined) {
      launchData.items[0].entry.file(

        function(result) {

          var reader = new FileReader();
          var XML;

          reader.onloadend = function(){
            XML = reader.result;
            chrome.storage.local.set({'XML': XML});
          };

          reader.readAsText(result);

        },

        function(){
          console.log("Error reading XML file");
        }

      );
    } else {
      console.log("No file was detected.");
    }
  });
});

检索 XML 的 dart 代码很简单:

String text = (await chrome.storage.local.get('XML'))['XML'];