Guest User

Untitled

a guest
Jan 16th, 2019
974
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.28 KB | None | 0 0
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'package:flutter/material.dart';
  4. import 'package:http/http.dart' as http;
  5.  
  6. void main() => runApp(MyApp());
  7.  
  8. class MyApp extends StatelessWidget {
  9.   @override
  10.   Widget build(BuildContext context) {
  11.     return MaterialApp(
  12.       home: MyHomePage(),
  13.     );
  14.   }
  15. }
  16.  
  17. class MyHomePage extends StatefulWidget {
  18.   @override
  19.   _MyHomePageState createState() => new _MyHomePageState();
  20. }
  21.  
  22. class _MyHomePageState extends State<MyHomePage> {
  23.   String url =
  24.       'https://my-json-server.typicode.com/hotgeart/app-api/categories';
  25.   List data;
  26.  
  27.   Future<String> makeRequest() async {
  28.     var response = await http
  29.         .get(Uri.encodeFull(url), headers: {"Accept": "application/json"});
  30.  
  31.     setState(() {
  32.       data = json.decode(response.body);
  33.     });
  34.   }
  35.  
  36.   @override
  37.   void initState() {
  38.     this.makeRequest();
  39.   }
  40.  
  41.   @override
  42.   Widget build(BuildContext context) {
  43.     return Scaffold(
  44.       appBar: AppBar(
  45.         title: Text('My App'),
  46.         backgroundColor: Colors.green,
  47.       ),
  48.       body: ListView.builder(                       // <----------  WORKING
  49.           itemCount: data == null ? 0 : data.length,
  50.           itemBuilder: (BuildContext context, i) {
  51.             return new ListTile(
  52.               title: new Text(data[i]["title"]),
  53.             );
  54.           }),
  55.       drawer: Drawer(
  56.         child: ListView(
  57.           // Important: Remove any padding from the ListView.
  58.           padding: EdgeInsets.zero,
  59.           children: <Widget>[
  60.             Container(
  61.               height: 85.0,
  62.               child: DrawerHeader(
  63.                 child: Text(
  64.                   'Categories',
  65.                   style: new TextStyle(fontSize: 18.0, color: Colors.white),
  66.                 ),
  67.                 decoration: BoxDecoration(
  68.                   color: Colors.green,
  69.                 ),
  70.               ),
  71.             ),
  72.             ListView.builder(                       // <---------- NOT WORKING
  73.                 itemCount: data == null ? 0 : data.length,
  74.                 itemBuilder: (BuildContext context, i) {
  75.                   return new ListTile(
  76.                     title: new Text(data[i]["title"]),
  77.                   );
  78.                 })
  79.           ],
  80.         ),
  81.       ),
  82.     );
  83.   }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment