2017-09-14 64 views
1

我的目标是尝试访问chrome上的浏览器沙盒文件系统,以及用户是否使用其他浏览器回退到其他代码。我试过如下:Dart:处理无法访问FileSystem的浏览器

print("Attempt to access filesystem"); 
window.requestFileSystem(1024 * 1024, persistent: false) 
    ..then((FileSystem) => print("successfully accessed FileSystem"),onError:(e) 
    { 
     print("failed to access FileSystem"); 
    }); 

测试这在Firefox与Chrome的我的问题是错误处理程序似乎并没有得到在Firefox达到(打印的唯一的事情就是“试图访问文件系统”)。我想知道问题是firefox不处理.then()语法。如果是这样的话,有人可以建议我如何检查浏览器是否支持Futures。一般来说,任何人都可以建议我如何实现这一目标?

+0

当我在Chrome [DartPad](https://dartpad.dartlang.org/580f89ede28d93b762ae1f44acd05ba7)中运行它时,我得到这个控制台输出'尝试访问文件系统''无法访问FileSystem' –

+0

是的,那是预期的输出。如果您现在尝试在Firefox中运行它 - 您应该只是“试图访问文件系统”,因此无法访问错误处理程序。我想知道如何让Firefox(或其他浏览器)到达错误处理程序? – SSS

回答

1

它更容易使用async/await

import 'dart:html'; 
main() async { 
    try { 
    print("Attempt to access filesystem"); 
    var fileSystem = await window.requestFileSystem(1024 * 1024, persistent: false); 
    print("successfully accessed FileSystem"); 
    } catch(e) { 
    print(e); 
    print("failed to access FileSystem"); 
    } 
} 

DartPad example

与您的代码到处理器错误try/catch也可以(除了onError因为错误的同步代码发生(方法window.requestFileSystem没有按(在Safari中)

DartPad example

+0

啊,我明白了 - 那太棒了。谢谢! – SSS

相关问题