JSON models and Practical use - Flutter

By pjain      Published May 10, 2020, 11:36 p.m. in blog Programming   

DataMgt,JSON 101

JSON

JSON Provider 101

The async and await keywords are part of the Dart languageā€™s asynchrony support.

Use await in an async function to pause execution until a Future completes

YTut1: Nitish

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
main(){runApp(new AppName()); }
class AppName extends StatefulWidget{
  @override State<StatefulWidget> createState() {return new AppState(); }
}

class AppState extends State<AppName>{
  String name = '';
  int id = 0;
  int public_repos = 0;
  TextEditingController user = new TextEditingController();
  String url = 'https://api.github.com/users/';

  getData(String username) async{
    String profile = url+username;
    var res = await http.get(profile,headers: {"Accept":"application/json"});
    var resBody = json.decode(res.body);
    name = resBody['name'];
    id = resBody['id'];
    public_repos = resBody['public_repos'];
    setState(() {
      print("Success");
    });
  }
    //Search form EnterGHuserName:___ <Get Info>
    //Rows for fetched: data Name ID PublicRepos
  @override Widget build(BuildContext context) { 
    return new MaterialApp(
      home: new Scaffold(
        appBar:  AppBar(title: new Text("JSON App"),),
        body: new Container(
          padding: EdgeInsets.all(12.0),
          child: new Column(
            children: <Widget>[
              new Padding(padding: EdgeInsets.only(bottom: 10.0)),
              new TextField(
                controller: user,
                decoration: InputDecoration(hintText: "Enter Github Uername"),
              ),
              new Padding(padding: EdgeInsets.only(bottom: 10.0)),
              new RaisedButton(
                  onPressed: ()=>getData(user.text),
                  child: new Text("Get Info")
              ),
              new Padding(padding: EdgeInsets.only(bottom: 20.0)),
              new Text("Name : $name"),
              new Padding(padding: EdgeInsets.only(bottom: 10.0)),
              new Text("ID : $id"),
              new Padding(padding: EdgeInsets.only(bottom: 10.0)),
              new Text("Public repo : $public_repos"),
            ],
          ),
        ),
      ),
    );
  }
}

0 comments

There are no comments yet

Add new comment

Similar posts

List++ Advanced Application Features

Interview Questions in Flutter

NAV: URL Launcher, Deep Links

NAV: Navigation Drawers