Advertisement
Guest User

json_list

a guest
Oct 20th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.57 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2.   import 'package:http/http.dart' as http;
  3.   import 'dart:async';
  4.   import 'dart:convert';
  5.  
  6.  class Lihat extends StatefulWidget {
  7.    @override
  8.    _LihatState createState() => _LihatState();
  9.  }
  10.  
  11.  class _LihatState extends State<Lihat> {
  12.    TextEditingController controllerCari = new TextEditingController();
  13.  Future<List> cariData() async {
  14.    final response = await http.get('https://rsudenisa736.000webhostapp.com/json_get.php?norm='+controllerCari.text);
  15.     return json.decode(response.body);
  16.    //http.Response response = await http.get('http://situs.com/json_get.php?id='+controllerCari.text);
  17.    //debugPrint(response.body);
  18.    
  19.    //var url =Uri.encodeFull('http://situs.com/json_get.php?id='+controllerCari.text);
  20.    //http.get(url);
  21.    
  22.  
  23.  }
  24.  
  25.  
  26.    @override
  27.    Widget build(BuildContext context) {
  28.      return Scaffold(
  29.        appBar: new AppBar(
  30.          title: new Text("Lihat Data"),  
  31.        ),
  32.  
  33.        body: new Container(
  34.        
  35.          /*future: cariData(),
  36.          builder: (context,snapshot){
  37.            if(snapshot.hasError) print(snapshot.error);
  38.            return snapshot.hasData ? new ItemList() : new Center(
  39.              child: new CircularProgressIndicator(),
  40.            );
  41.          }, */
  42.  
  43.            
  44.           child : new Column(
  45.         children: <Widget>[
  46.    
  47.            new TextField(
  48.                controller: controllerCari,
  49.                decoration: new InputDecoration(
  50.                  hintText: "Cari Data"
  51.                ),
  52.  
  53.              ),
  54.  
  55.               new RaisedButton(
  56.                 onPressed: (){
  57.                   cariData();
  58.                 },
  59.                 child: const Text("Cari DATA"),
  60.                 textColor: Colors.blue,
  61.               ),
  62.          
  63.             new FutureBuilder<List>(
  64.                 future: cariData(),
  65.          builder: (context,snapshot){
  66.            if(snapshot.hasError) print(snapshot.error);
  67.            return snapshot.hasData ? new ItemList(list: snapshot.data,) : new Center(
  68.              child: new CircularProgressIndicator(),);
  69.          },
  70.             ),
  71.            
  72.          
  73.         ],
  74.  
  75.              
  76.      
  77.        
  78.        ),
  79.        ),
  80.      
  81.        
  82.      );
  83.    }
  84.  }
  85.  class ItemList extends StatelessWidget {
  86.    final List list;
  87.    ItemList({this.list});
  88.    @override
  89.    Widget build(BuildContext context) {
  90.      return ListView.builder(
  91.        itemCount: list==null ? 0 : list.length,
  92.        itemBuilder: (context, i){
  93.          return new Text(list[i]['norm']);
  94.        }
  95.        
  96.      );
  97.    }
  98.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement