2017-09-14 63 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

相關問題