2015-03-13 35 views
0

我試圖按照鏢風格指南我的鏢代碼,但我不確定這一點:飛鏢式的指導

library ch_panel.section_model; 

import 'package:ch_padart/models/page.dart'; 

class Section { 

    String icon; 
    String title; 
    List<Page> pages; 

    Section(this.icon, this.title, this.pages); 
    Section.fromJson(Map<String, dynamic> json): this(json['icon'], json['title'], parsePages(json['pages'])); 

    static List<Page> parsePages(List<Map> pageJson) { 
    List<Page> itemPages = new List(); 

    pageJson.forEach((v) { 
     itemPages.add(new Page(v['icon'], v['title'])); 
    }); 

    return itemPages; 
    } 
} 

風格指引我指的是: https://www.dartlang.org/articles/style-guide/#members

你可以重新做這個代碼作爲在這種情況下正確使用樣式的例子?

+2

你是在大約疑問怎麼辦? – 2015-03-13 16:32:11

+0

我不確定我是否應該在構造函數中使用靜態函數。 – 2015-03-13 16:33:56

+0

你是什麼意思「構造函數中的靜態函數」。最好使用靜態方法的構造函數*而不是*。你的代碼很好。 – 2015-03-13 16:35:26

回答

3

可以改進的是格式。 Dartformat自動執行此操作(應該集成到DartEditor中)。您也可以從命令行手動運行它。

# install 
pub global activate dart_style 
# run 
pub global run dartformat -w myfile.dart 
# or to format a whole directory incl subdirs 
pub global run dartformat -w . 
library ch_panel.section_model; 

import 'package:ch_padart/models/page.dart'; 

class Section { 
    String icon; 
    String title; 
    List<Page> pages; 

    Section(this.icon, this.title, this.pages); 
    Section.fromJson(Map<String, dynamic> json) 
     : this(json['icon'], json['title'], parsePages(json['pages'])); 

    static List<Page> parsePages(List<Map> pageJson) { 
    List<Page> itemPages = new List(); 

    pageJson.forEach((v) { 
     itemPages.add(new Page(v['icon'], v['title'])); 
    }); 

    return itemPages; 
    } 
} 

還有一個進展中的工作棉短絨檢查其違反風格(非常早期的階段,但已經可以使用)

# install 
pub global activate -s git [email protected]:dart-lang/linter.git 
# run 
pub global run linter myfile.dart 
+0

我無法在酒吧找到短笛 – Anders 2015-03-13 23:52:57

+0

你說得對。我在我的電腦上試過了,輸出看起來不像是一個錯誤信息,因爲我已經安裝了運行它的工作。我更新了我的答案。 – 2015-03-14 08:18:48