Skip to content

Commit

Permalink
Added option to call the TMDB API with key from env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
anoobbava committed Jul 24, 2020
1 parent 05a7379 commit 8bd36ce
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
.buildlog/
.history
.svn/
.env

# IntelliJ related
*.iml
Expand Down
18 changes: 10 additions & 8 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import 'dart:convert';

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

Future<Album> fetchAlbum() async {
final String keyVar = DotEnv().env['MOVIE_KEY'];
final response =
await http.get('https://jsonplaceholder.typicode.com/albums/1');
await http.get("https://api.themoviedb.org/3/movie/551?api_key=$keyVar");

if (response.statusCode == 200) {
// If the server did return a 200 OK response,
Expand All @@ -20,22 +22,22 @@ Future<Album> fetchAlbum() async {
}

class Album {
final int userId;
final int id;
final String title;

Album({this.userId, this.id, this.title});
Album({this.title});

factory Album.fromJson(Map<String, dynamic> json) {
return Album(
userId: json['userId'],
id: json['id'],
title: json['title'],
title: json['original_title'],
);
}
}

void main() => runApp(MyApp());
Future main() async {
await DotEnv().load('.env');
runApp(MyApp());
}
// runApp(MyApp());

class MyApp extends StatefulWidget {
MyApp({Key key}) : super(key: key);
Expand Down
7 changes: 7 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_dotenv:
dependency: "direct main"
description:
name: flutter_dotenv
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
flutter_test:
dependency: "direct dev"
description: flutter
Expand Down
8 changes: 5 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies:
sdk: flutter
cupertino_icons: ^0.1.3
http: ^0.12.2
flutter_dotenv: ^2.1.0

dev_dependencies:
flutter_test:
Expand All @@ -21,9 +22,10 @@ flutter:
uses-material-design: true

# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
assets:
- .env
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg

# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.
Expand Down

0 comments on commit 8bd36ce

Please sign in to comment.