handle events api
This commit is contained in:
parent
2120e24a79
commit
cf29b1d364
@ -1,9 +1,12 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'classes/post.dart';
|
||||
import 'classes/events.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'variable/globals.dart' as globals;
|
||||
|
||||
// app starting point
|
||||
void main() {
|
||||
runApp(const MyApp());
|
||||
@ -32,20 +35,21 @@ class ListItemMenu extends StatefulWidget {
|
||||
// homepage state
|
||||
class _MyHomePageState extends State<ListItemMenu> {
|
||||
// variable to call and store future list of posts
|
||||
Future<List<Post>> postsFuture = getPosts();
|
||||
Future<List<Events>> postsFuture = getPosts();
|
||||
|
||||
// function to fetch data from api and return future list of posts
|
||||
static Future<List<Post>> getPosts() async {
|
||||
static Future<List<Events>> getPosts() async {
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
var accessToken = prefs.getString("access_token") ?? "";
|
||||
final List<Post> body = [];
|
||||
final List<Events> body = [];
|
||||
if (accessToken.isNotEmpty) {
|
||||
var url =
|
||||
Uri.parse("https://jsonplaceholder.typicode.com/albums/1/photos");
|
||||
final response =
|
||||
await http.get(url, headers: {"Content-Type": "application/json"});
|
||||
final List body = json.decode(response.body);
|
||||
return body.map((e) => Post.fromJson(e)).toList();
|
||||
var url = Uri.parse("http://${globals.api}/events");
|
||||
final response = await http.get(url, headers: {
|
||||
"Content-Type": "application/json",
|
||||
HttpHeaders.cookieHeader: "access_token=${accessToken}"
|
||||
});
|
||||
final List body = json.decode(utf8.decode(response.bodyBytes));
|
||||
return body.map((e) => Events.fromJson(e)).toList();
|
||||
}
|
||||
return body;
|
||||
}
|
||||
@ -56,7 +60,7 @@ class _MyHomePageState extends State<ListItemMenu> {
|
||||
return Scaffold(
|
||||
body: Center(
|
||||
// FutureBuilder
|
||||
child: FutureBuilder<List<Post>>(
|
||||
child: FutureBuilder<List<Events>>(
|
||||
future: postsFuture,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
@ -77,7 +81,7 @@ class _MyHomePageState extends State<ListItemMenu> {
|
||||
}
|
||||
|
||||
// function to display fetched data on screen
|
||||
Widget buildPosts(List<Post> posts) {
|
||||
Widget buildPosts(List<Events> posts) {
|
||||
// ListView Builder to show data in a list
|
||||
return ListView.builder(
|
||||
itemCount: posts.length,
|
||||
@ -85,15 +89,20 @@ class _MyHomePageState extends State<ListItemMenu> {
|
||||
final post = posts[index];
|
||||
return Container(
|
||||
color: Colors.grey.shade300,
|
||||
margin: EdgeInsets.symmetric(vertical: 5, horizontal: 10),
|
||||
margin: EdgeInsets.symmetric(vertical: 5, horizontal: 5),
|
||||
padding: EdgeInsets.symmetric(vertical: 5, horizontal: 5),
|
||||
height: 100,
|
||||
width: double.maxFinite,
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(flex: 1, child: Image.network(post.url!)),
|
||||
SizedBox(width: 10),
|
||||
Expanded(flex: 3, child: Text(post.title!)),
|
||||
Text(
|
||||
'${post.name!}\n\n',
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
Text(
|
||||
'${post.place!}',
|
||||
style: Theme.of(context).textTheme.titleSmall,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
13
covas_mobile/lib/classes/events.dart
Normal file
13
covas_mobile/lib/classes/events.dart
Normal file
@ -0,0 +1,13 @@
|
||||
class Events {
|
||||
String? id;
|
||||
String? name;
|
||||
String? place;
|
||||
|
||||
Events({this.place, this.id, this.name});
|
||||
|
||||
Events.fromJson(Map<String, dynamic> json) {
|
||||
id = json['id'];
|
||||
name = json['name'];
|
||||
place = json['place'];
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user