Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:async';
- import 'dart:convert';
- import 'package:flutter/material.dart';
- import 'package:http/http.dart' as http;
- void main() => runApp(MyApp());
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- home: MyHomePage(),
- );
- }
- }
- class MyHomePage extends StatefulWidget {
- @override
- _MyHomePageState createState() => new _MyHomePageState();
- }
- class _MyHomePageState extends State<MyHomePage> {
- String url =
- 'https://my-json-server.typicode.com/hotgeart/app-api/categories';
- List data;
- Future<String> makeRequest() async {
- var response = await http
- .get(Uri.encodeFull(url), headers: {"Accept": "application/json"});
- setState(() {
- data = json.decode(response.body);
- });
- }
- @override
- void initState() {
- this.makeRequest();
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text('My App'),
- backgroundColor: Colors.green,
- ),
- body: ListView.builder( // <---------- WORKING
- itemCount: data == null ? 0 : data.length,
- itemBuilder: (BuildContext context, i) {
- return new ListTile(
- title: new Text(data[i]["title"]),
- );
- }),
- drawer: Drawer(
- child: ListView(
- // Important: Remove any padding from the ListView.
- padding: EdgeInsets.zero,
- children: <Widget>[
- Container(
- height: 85.0,
- child: DrawerHeader(
- child: Text(
- 'Categories',
- style: new TextStyle(fontSize: 18.0, color: Colors.white),
- ),
- decoration: BoxDecoration(
- color: Colors.green,
- ),
- ),
- ),
- ListView.builder( // <---------- NOT WORKING
- itemCount: data == null ? 0 : data.length,
- itemBuilder: (BuildContext context, i) {
- return new ListTile(
- title: new Text(data[i]["title"]),
- );
- })
- ],
- ),
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment