2014-10-02 28 views
2

同伴飛鏢程序員。如何檢查Dart中Stream的結束?

我在使用Stream讀取文件如下。

Stream<List<int>> stream = new File(filepath).openRead(); 
stream 
    .transform(UTF8.decoder) 
    .transform(const LineSpilitter()) 
    .listen((line){ 
     // TODO: check if this is the last line of the file 
     var isLastLine; 
    }); 

我要檢查在聽行()是否是文件的最後一行。

回答

3

我不認爲你可以檢查當前數據塊是否是最後一塊。
您只能傳遞在流關閉時調用的回調函數。

Stream<List<int>> stream = new File('main.dart').openRead(); 
    stream. 
    .transform(UTF8.decoder) 
    .transform(const LineSpilitter()) 
    .listen((line) { 
// TODO: check if this is the last line of the file 
    var isLastLine; 
    } 
    ,onDone: (x) => print('done')); // <= add a second callback 
+0

感謝您的回答。當解析一個文本文件時,我需要一種方式來處理最後一個段比以前有所不同。我不能使用onDone,因爲它是全部「完成」流,所以變量'line'中的文本不再可用。 – seongjoo 2014-10-02 06:13:01

+2

我明白了。你可以做的是緩衝最後一行,並在收到下一行後才處理它。 'onDone'你處理最後的緩衝線。 – 2014-10-02 06:14:55

+0

我認爲這可以工作。如果我必須在沒有解決方法的情況下執行此操作,則可能需要直接處理來自File的字節而不使用Stream。 – seongjoo 2014-10-03 03:13:31

相關問題