Flutter Http Get Movie DB

Flutter Http Get Movie DB

API Movie DB : http://api.themoviedb.org/3/movie/top_rated?api_key=e5013e88aad0bad4821bdac93d1d6a30

pubspec.yaml

http: 0.12.0+2

main.dart

import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;

void main() => runApp(
  new MaterialApp(
    home: App(),
    debugShowCheckedModeBanner: false,
  )
);

class App extends StatefulWidget {
  AppState createState() => AppState();
}

class AppState extends State<App> {
  final String url =
      'http://api.themoviedb.org/3/movie/top_rated?api_key=e5013e88aad0bad4821bdac93d1d6a30';
  final String image_url = "https://image.tmdb.org/t/p/w600_and_h900_bestv2/";
  List data;

  Future<String> getData() async {
    var res = await http
        .get(Uri.encodeFull(url), headers: {'accept': 'application/json'});
    setState(() {
      var content = json.decode(res.body);
      data = content['results'];
    });
    return 'success!';
  }

  @override
  void initState() {
    super.initState();
    this.getData();
  }

  Widget build(context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Movie DB',
      home: Scaffold(

          //HEADER
          appBar: AppBar(title: Text('Movie DB')),

          //CONTENT
          body: Container(
            margin: EdgeInsets.all(10.0),

            //LIST VIEW
            child: ListView.builder(
              itemCount: data == null ? 0 : data.length,
              itemBuilder: (BuildContext context, int index) {
                return Container(
                    //CARD
                    child: Card(
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      //LIST TILE
                      ListTile(

                        leading: Image.network(
                          this.image_url + data[index]['poster_path'],
                          fit: BoxFit.contain,
                        ),

                        title: Text(
                          data[index]['title'],
                          style: TextStyle(
                              fontSize: 15.0, fontWeight: FontWeight.bold),
                        ),

                        subtitle: Column(
                          children: <Widget>[

                            Row(
                              children: <Widget>[
                                Text(
                                  'Language : ',
                                  style: TextStyle(fontWeight: FontWeight.bold),
                                ),
                                Text(
                                  data[index]['original_language'],
                                  style: TextStyle(
                                      fontStyle: FontStyle.italic,
                                      fontSize: 15.0),
                                ),
                              ],
                            ),


                            Row(
                              children: <Widget>[
                                Text(
                                  'Vote : ',
                                  style: TextStyle(fontWeight: FontWeight.bold),
                                ),
                                Text(data[index]['vote_average'].toString()),
                                Icon(
                                  Icons.star,
                                  size: 20.0,
                                  color: Colors.yellow,
                                ),
                              ],
                            ),


                            Row(
                              children: <Widget>[
                                Text(
                                  'Popular : ',
                                  style: TextStyle(fontWeight: FontWeight.bold),
                                ),
                                Text(
                                  data[index]['popularity'].toString(),
                                  softWrap: false,
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),

                      //BUTTON
                      ButtonTheme.bar(
                        child: ButtonBar(
                          children: <Widget>[
                            FlatButton(
                              child: const Text('Detail'),
                              onPressed: () {},
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ));
              },
            ),
          )),
    );
  }
}

Movie DB

Flutter Developer