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