Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Random-gif #39

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions Random_Gif.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
````
import 'dart:async';
import 'dart:convert';

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

Future<Post> fetchPost() async {
final response =
await http.get('https://api.giphy.com/v1/gifs/random?api_key=vu0Kzk1BktOH6Gp0836mAoWSKwbcWqBZ');

if (response.statusCode == 200) {
// If the call to the server was successful, parse the JSON
return Post.fromJson(json.decode(response.body));
} else {
// If that call was not successful, throw an error.
throw Exception('Failed to load post');
}
}

class Post {
final int gif;
Post({this.gif});

factory Post.fromJson(Map<String, dynamic> json) {
return Post(
gif: json['Random'],
);
}
}

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Random Gif',
theme: ThemeData(
primarySwatch: Colors.red,
),
home: Scaffold(
appBar: AppBar(
title: Text('Random gif'),
),
body: Center(
child: FutureBuilder<Post>(
future: fetchPost(),
builder: (context, snapshot) {
if (snapshot.hasError) {
return Text("${snapshot.error}");
}

// By default, show a loading spinner
return CircularProgressIndicator();
},
),
),
),
);
}
}
````