Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dart for Android Devs

Dart for Android Devs

shibuya.apk #26で発表した内容です。Android開発者として、Flutterを学ぶ際にDartでおやっと思ったポイントを軽くまとめました。

Shigehiro Soejima

June 27, 2018
Tweet

Other Decks in Programming

Transcript

  1. σʔλܕ numbers • int int x = 1; int hex

    = 0xDEADBEEF; • double double y = 1.1; double exponents = 1.42e5; int x = 4 / 2; // error int y = 5 ~/ 2; // y = 2 • num: int or double num z = 4 / 2; // z = 2.0 (double) z = 2; // z = 2 (int), no error
  2. σʔλܕ strings • UTF-16 • Singe/Double ΫΦʔτͲͪΒͰ΋Մ var str1 =

    ‘Hello world!’; var str2 = "Hello world!”; • Raw จࣈྻ var raw = r’\t\t\t'; print(raw); // \t\t\t
  3. • ࿈݁ (վߦɺϒϥϯΫɺʴΛ࢖ͬͨ3௨Γ) var s1 = 'Hello ' "world!"; print(s1);

    // Hello world! var s2 = 'Hello ' "world!"; print(s2); // Hello world! var s3 = 'Hello ' + "world!"; print(s3); // Hello world! σʔλܕ strings s1, s2, s3ͷग़ྗ݁Ռ͸͢΂ͯಉ͡ɻ
  4. • 3࿈ΫΦʔτʢ’’’΋͘͠͸”””ʣͰғΉ͜ͱͰෳ਺ߦΛ ··දݱՄɻ var str = '''Hello world!'''; print(str); var

    x = 3; print("number=$x"); // number=3 var str = "Hello world!"; print("length=${str.length}"); // length=12 • ิؒ σʔλܕ strings
  5. σʔλܕ list = array var list = [1, 2, 3,

    4, 5]; print(list[0]); // 1 (index starts at 0) list[0] = 0; print(list); // [0, 2, 3, 4, 5] list[0] = 2.0; // error var list2 = const [6, 7, 8, 9, 0]; const list3 = [6, 7, 8, 9, 0]; • ఆ਺ʹ͢Δʹ͸constΛ͍ͣΕ͔ͷํ๏Ͱ෇͚Δɻ
  6. σʔλܕ map var map1 = Map<int, String>(); // new is

    optional map1[0] = "zero"; map1[1] = "one"; const map4 = {0: "zero", 1: “one"}; var map3 = const {0: "zero", 1: "one"}; • constԽ͸Listܕʹಉ͡ɻ var map2 = { "cero": "zero", "uno": "one", "dos": "two"}; • key-valueϖΞͰͷఆٛ΋Մೳɻ
  7. ม਺ɾఆ਺ var num1 = 1; final num3; const num4 =

    4; int num2 = 2; final int num5; const int num6 = 6; • var ΋͘͠͸໌ࣔతʹܕΛ༩͑ͯఆٛ͢Δɻ • constͱfinal͸ͲͪΒ΋ఆ਺Λఆٛ͢Δ͕ɺ const͸ίϯύΠ ϧ࣌ʹ஋͕ඞཁͰɺfinal͸ίϯύΠϧ࣌ʹ஋͸ෆཁ͕ͩҰ౓ ஋Λ༩͑ΔͱҎ߱มߋෆՄɻ • _num0ͷΑ͏ʹΞϯμʔείΞΛ෇͚Δͱprivateͷѻ͍ʹͳ Δʢϝιου΋ಉ͡ʣɻ
  8. ੍ޚจ • if else • for loop • while loop

    • do-while loop • switch • forEach
  9. ੍ޚจ Java • if else • for loop • while

    loop • do-while loop • switch ͜ΕΒ͸Javaʹجຊಉ͡ɻࡾ߲ԋࢉࢠ΋ར༻Մɻbreak, continue΋ಉ͡ɻ
  10. ੍ޚจ forEach var list = [1, 2, 3, 4, 5];

    list.forEach((elm) => print(elm)); • ΋͏গ͠ෳࡶͳॲཧΛߦ͍͍ͨ৔߹͸ؔ਺Λ࡞ͬͯ౉͢ɻ void printKeyValue(key, value) { if (key != null && value != null) { print("$key:$value"); } } Map map = {0: "zero", 1: "one"}; map.forEach(printKeyValue);
  11. try on try { var list = [1, 2, 3];

    print(list[3]); } on IndexError { print("Index out of bounds :("); } • catchΛ͚ͭͯྫ֎಺༰ΛऔΔ͜ͱ΋Մɻ try { var list = [1, 2, 3]; print(list[3]); } on IndexError catch (e) { print("Index out of bounds: $e"); }
  12. try catch try { somethingGoesWrong(); } on ExceptionA { //

    do something here } on ExceptionB { // do something here } catch (e) { // do something here } • Catch-allͰ͢΂ͯิ଍͢Δɻ
  13. Ϋϥε class Cat { String breed; int age; String name;

    Cat(String breed, int age, String name) { this.breed = breed; this.age = age; this.name = name; } } var birman = Cat("Birman", 13,"Ryu"); birman.age = 14;
  14. Ϋϥε class Cat { final String breed; final int age;

    final String name; Cat(this.breed, this.age, this.name); } • Cat(this.breed, this.age, this.name) ͷΑ͏ʹthis.ύϥϝʔ λ໊ͱ͢Δ͜ͱͰίϯετϥΫλͷ୅ೖॲཧͷهड़Λল ུͰ͖ɺfinalΛ࢖͏͜ͱ΋Ͱ͖Δɻ
  15. Ϋϥε Named Constructor class AndroidEvent { String title; String date;

    AndroidEvent(this.title, this.date); AndroidEvent.shibuyaApk(this.date) { this.title = "Shibuya.apk"; this.date = date; } AndroidEvent.caApk(this.date) { this.title = “CA.apk"; this.date = date; } } var event = AndroidEvent.shibuyaApk("today");
  16. Ϋϥε Factory Constructor class Series { final int id; final

    String title; final String url; Series(this.id, this.title, this.url); factory Series.fromJson(Map<String, dynamic> json) { return new Series(json['id'], json['title'], json['url']); } } • ໊લ෇͖ίϯετϥΫλʹࣅ͍ͯΔ͕ɺதͰΠϯελϯε Λ࡞ͬͯฦ͢ඞཁ͕͋Δɻ
  17. Ϋϥε Inheritance class Cat { String breed; int age; String

    name; Cat({String breed, int age, String name}) { this.breed = breed; this.age = age; this.name = name; } } var birman = new Cat(breed: "Birman", age: 13, name: "Ryu"); class Birman extends Cat { Birman(int age, String name) : super(breed: "Birman", age: age, name: name); Birman.myCat() : this(1, "Shuri"); } var birman = Birman(1, "Kairi");
  18. ؔ਺ var _counter = 0; void increment() { _counter++; }

    void increment() => _counter++; • ؔ਺ͷத਎͕͚ࣜͩͷ΋ͷ͸ɺলུܗͰ΋هड़Մɻ
  19. ؔ਺ getters and setters var _counter = 0; void increment()

    { _counter++; } catName = "Tama"; print(catName); // TAMA print(name); // Tama class Cat { final String breed; final int age; final String name; Cat(this.breed, this.age, this.name); String get catName => this.name.toUpperCase(); set catName(String name) => this.name = name; }
  20. ؔ਺ Default/Named parameters void increment(int amount = 1) { ...

    } increment(); • σϑΥϧτύϥϝʔλ • ໊લ෇͖ύϥϝʔλ void enableFlags({bool bold = false, bool hidden = false}) { ... } enableFlags(hidden: true); {}ͰғΉɻશମͰͳ͘ݸผʹғΉ͜ͱ΋Մɻ
  21. ؔ਺ Positional parameters • ϙδγϣφϧύϥϝʔλ void setName(String last, [String first,

    String middle]) { // } []Ͱғͬͨύϥϝʔλ܈͸ͲΕ͔̍ͭʹ஋Λ༩͑ͨΒɺͦͷύϥϝʔλΑΓॱ ൪͕લͷ΋ͷ΋஋Λ༩͑Δඞཁ͕͋Δɻ ྫ͑͹ɺ[a, b, c, d, e] ͱ͋ͬͨͱ͢Δͱɺdʹ஋Λ༩͑Δ৔߹͸a, b, cʹ΋༩͑ ͳ͚ΕͳΒͳ͍ɻ