Print
Details: Category: บทความ Flutter | Published: 01 February 2019 | Hits: 4983

เป็นบนเรียนหนึ่งใน คอร์ส สอน Flutter ตัวต่อตัว นี่ครับ https://www.androidthai.in.th/private-project-android-flutter-course.html 

 Screen Shot 2562 02 01 at 134224

 

 โจทย์เป็นอย่างนี่ เดี๋ยวเราจะอ่านค่า จาก https://jsonplaceholder.typicode.com/photos/1 

Screen Shot 2562 02 01 at 134147

 

งั้นเราต้อง Add Library ที่สามารถ อ่านอะไร ? จาก http ได้ก่อนครับ

 Screen Shot 2562 02 01 at 134155

 ไฟร์ นี่แหละครับ

 Screen Shot 2562 02 01 at 134207

เพิ่มคำสั่งนี่เข้าไป http: any 

Screen Shot 2562 02 01 at 134211

สังเกตุที่ Terminal จะ มีการทำ flutter packages get แบบนี้ครับ

 Screen Shot 2562 02 01 at 134217

ทีนี่เราก็ import package:http ได้ละ

Screen Shot 2562 02 01 at 134236

แต่อย่าลืมว่าเราต้องเปลี่ยน object JSON ที่อ่านได้ ไปเป็น String จึงต้องมีการใช้ Library dart:convert ให้ import ด้วยดครับ

Screen Shot 2562 02 01 at 134242

 

ที่ เมธอด ที่มาสเตอร์ จะใช้อ่าน JSON จะทำเป็น Thread async แบบนี่ 

Screen Shot 2562 02 01 at 134248

ใน เมธอด ต้อง setState ด้วยครับ

Screen Shot 2562 02 01 at 134255

ลองทดสอบ มาสเตอร์​ให้ แสดงสิ่งที่อ่านได้ ใน Console แบบนี้ครับ ไม่ยากนะครับ สำหรับใครที่ทำไม่ได้อย่างไร ? ลองมาเรียน Flutter กันตัวต่อตัวทีนี่ครับ https://www.androidthai.in.th/private-project-android-flutter-course.html