ไม่สามารถเล่นวิดีโอนี้
ขออภัยในความไม่สะดวก

Flutter - Upgrading to Dart 2, JSON & Casting

แชร์
ฝัง
  • เผยแพร่เมื่อ 30 มี.ค. 2018
  • In this episode I cover upgrading the application I built earlier on to Dart 2. I found that JSON had been changed to JSON. And then I corrected the casting for the Dart 2 compiler.
    This is the episode I built the application in:
    • Flutter - Loading page...
    In this episode I cover:
    - JSON is renamed to json
    - Implicit to explicit casting
    - Casting to a list
    - Casting to a map
    * During the recording I was using the master Flutter channel.
    Source code used:
    gist.github.com/branflake2267...
    Why was the type system changed?
    • Sound Dart (Dart Devel...
    And more on Dart 2:
    www.dartlang.org/dart-2

ความคิดเห็น • 31

  • @TensorProgramming
    @TensorProgramming 6 ปีที่แล้ว +2

    Very nice explanation. Its nice to see other developers covering flutter here on TH-cam.

    • @BrandonDonnelson
      @BrandonDonnelson  6 ปีที่แล้ว

      Thank you. Thanks for the feedback. I see you're doing it too, nice job!

  • @brianrossegan
    @brianrossegan 6 ปีที่แล้ว +3

    Nice video, thanks for sharing! I've been doing this a lot the past few days as well, and have found the `cast` method on List to be quite handy as well.
    E.g. `List list = new JsonCodec().decode(json).cast();`
    Not sure if this is better or worse than what you got, but thought I should share and see if anyone else has even better techniques!

    • @BrandonDonnelson
      @BrandonDonnelson  6 ปีที่แล้ว +1

      Thanks for sharing! I had Googler email this method as well, although you could called lower case json variable in the library, which is a compiled constant variable, so instead of instantiating another instance of JsonCodec it could be: `json.decode(json).cast();`. Nice job!

    • @BrandonDonnelson
      @BrandonDonnelson  6 ปีที่แล้ว +1

      So your syntax is much more elegant and I need to fix what I said a little... `json.decode(jsonString).cast();`

    • @brianrossegan
      @brianrossegan 6 ปีที่แล้ว

      Ah, good call! I think I started from the wrong sample and forgot to make the updates shown in the vid.

    • @BrandonDonnelson
      @BrandonDonnelson  6 ปีที่แล้ว

      I looked back and had it wrong in the gist and had to fix it too. Someone else had another shortcut, so I'm going to share that too.
      List list = json.decode(jsonString).cast();
      // or
      //List list = jsonDecode(jsonString).cast();
      var datas = new List();
      list.forEach((Map map) {
      datas.add(new Data.fromMap(map));
      });

    • @TensorProgramming
      @TensorProgramming 6 ปีที่แล้ว +2

      If you really want to clean up the code you can make it a single line function by doing something like this:
      List datas = await _getJson(offset, limit).then((jsonStr) => json.decode(jsonStr).cast().map((data) => Data.fromMap(data))) ;
      (you can cast it as a map or list in this case and it doesn't really matter).
      Or even better, rather then making a single variable, you could just return the await _getJson etc etc. Maybe its because I am a functional nut but I find using 3 different variables as go-betweens a little wasteful.
      Great tutorial non-the-less. I do like that there are other people covering Flutter on TH-cam.

  • @thedarkside0007
    @thedarkside0007 6 ปีที่แล้ว

    should i start developing in flutter or wait for more stable dart/flutter

  • @KashifMinhaj
    @KashifMinhaj 6 ปีที่แล้ว

    So we need to basically change things to dynamic type now. :(

    • @BrandonDonnelson
      @BrandonDonnelson  6 ปีที่แล้ว

      I know the feeling, I've felt the same way, migration is painful. This video tells the story of why the change occurred th-cam.com/video/DKG5CMyol9U/w-d-xo.html&t.

    • @BrandonDonnelson
      @BrandonDonnelson  6 ปีที่แล้ว +2

      Just in case, here's a quick list, for those who stop by of the changes, www.dartlang.org/guides/language/sound-problems

  • @victorsejas1726
    @victorsejas1726 6 ปีที่แล้ว +1

    Good tutorials!, I have a questio,n how can I rebuild only a widget and not the whole page?

    • @BrandonDonnelson
      @BrandonDonnelson  6 ปีที่แล้ว +1

      Flutter takes care of incremental building on the framework side. All you have to do is construct the view and change the state. I do have a video called swapping widgets, maybe that would help provide another option.

    • @BrandonDonnelson
      @BrandonDonnelson  6 ปีที่แล้ว

      Thanks for your feedback!

  • @WilliamShakour
    @WilliamShakour 6 ปีที่แล้ว +1

    you should be alright with just using List, keys in json object can only ever be strings... I think that is the issue.

    • @BrandonDonnelson
      @BrandonDonnelson  6 ปีที่แล้ว

      I found in Dart 2, I couldn't use implicit casting any longer. Since the return of the json decode call was dynamic, I had to tell the compiler which type I wanted it to be transformed into.

    • @WilliamShakour
      @WilliamShakour 6 ปีที่แล้ว

      Very odd... it is working fine for me. The only difference between the code you are showing and mine is that I am using the jsonDecode method. I cannot imagine that is it!
      Map map = jsonDecode(stringValue);

    • @BrandonDonnelson
      @BrandonDonnelson  6 ปีที่แล้ว

      Gotcha. Try this, run `flutter channel master` double check with `flutter upgrade` when it's switched. This will run with dart 2 and my guess is that will throw an exception. Or maybe you are running on the master already?

    • @WilliamShakour
      @WilliamShakour 6 ปีที่แล้ว

      billy1380@ROGLINUXWILL:~/git/dart_service_client$ flutter channel master
      Switching to flutter channel 'master'...
      git: From github.com/flutter/flutter
      git: * [new branch] Hixie-patch-2 -> origin/Hixie-patch-2
      git: 3352a3fb4..1d067220d dev -> origin/dev
      git: c5288c734..68728e9f9 master -> origin/master
      git: * [new tag] v0.2.6 -> v0.2.6
      git: * [new tag] v0.2.5 -> v0.2.5
      git: Switched to a new branch 'master'
      git: Branch master set up to track remote branch master from origin.
      billy1380@ROGLINUXWILL:~/git/dart_service_client$ flutter upgrade
      Downloading Dart SDK from Flutter engine c903c217a1a8206cdebdab1703b52ec6180edf37...
      % Total % Received % Xferd Average Speed Time Time Time Current
      Dload Upload Total Spent Left Speed
      100 43.7M 100 43.7M 0 0 3203k 0 0:00:14 0:00:14 --:--:-- 3267k
      Building flutter tool...
      Upgrading Flutter from /home/billy1380/flutter...
      Already up-to-date.
      Upgrading engine...
      Downloading package sky_engine...
      Flutter 0.2.7-pre.8 • channel master • github.com/flutter/flutter.git
      Framework • revision 68728e9f98 (2 hours ago) • 2018-04-02 21:28:40 +0300
      Engine • revision c903c217a1
      Tools • Dart 2.0.0-dev.43.0.flutter-52afcba357
      Running "flutter packages upgrade" in dart_service_client... 11.4s
      Running flutter doctor...
      Doctor summary (to see all details, run flutter doctor -v):
      [✓] Flutter (Channel master, v0.2.7-pre.8, on Linux, locale en_GB.UTF-8)
      [✓] Android toolchain - develop for Android devices (Android SDK 26.0.2)
      [✓] Android Studio (version 3.1)
      [✓] VS Code (version 1.21.1)
      [✓] Connected devices (1 available)
      • No issues found!
      billy1380@ROGLINUXWILL:~/git/dart_service_client$ flutter build apk
      Initializing gradle... 0.7s
      Resolving dependencies... 1.5s
      Running 'gradlew assembleRelease'...
      Skipping AOT snapshot build. Fingerprint match.
      Built build/app/outputs/apk/release/app-release.apk (9.1MB).
      I put it on the device (flutter install) and it works... :D

    • @BrandonDonnelson
      @BrandonDonnelson  6 ปีที่แล้ว

      Nice job. That's good to know. It doesn't look like the run time checks are being used when install. I noticed the exceptions when the return type was dynamic and implicitly implied on the consumer side of the variable declaration. And I was testing in Android Studio, so I'm not sure the type system has been updated in the other IDEs yet. Could you try out your method in the IDE, debugging from the IDE? I'd be curious if it works there too.