인프런 - 강의/플루터로 앱 만들기 고고(입문)

23 - API - 2

----___<<<<< 2020. 5. 21. 21:43

 

import 'package:flutter/material.dart';

import 'dart:convert';
import 'package:http/http.dart' as http;

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: gitAPI(),
    );
  }
}

class gitAPI extends StatefulWidget {
  @override
  _gitAPIState createState() => _gitAPIState();
}

class _gitAPIState extends State<gitAPI> {

  List data;
  String git_data_convert = "뿌우";

  void getData() async {
    var response = await http.get(
      Uri.encodeFull("https://api.github.com/search/users?q=chan"),
      headers: {
          "Accept" : "application/json"
      }
    );

//    print(response.body);
    data = json.decode(response.body)["items"];
    print(data[1]["login"]);

    setState(() {
      git_data_convert = data[2]["login"];
    });

  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: RaisedButton(
          child: Text(git_data_convert),
          onPressed: getData,
        ),
      )

    );
  }
}

'인프런 - 강의 > 플루터로 앱 만들기 고고(입문)' 카테고리의 다른 글

22 - API - 1  (0) 2020.05.21
21 - Todo App - 4  (0) 2020.05.07
20 - Todo App - 3  (0) 2020.05.07
19 - Todo App - 2  (0) 2020.05.07
18 - Todo App - 1  (0) 2020.05.07