feature/itemlist #2
@ -2,6 +2,7 @@ import 'dart:convert';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'classes/post.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
// app starting point
|
||||
void main() {
|
||||
@ -35,12 +36,19 @@ class _MyHomePageState extends State<ListItemMenu> {
|
||||
|
||||
// function to fetch data from api and return future list of posts
|
||||
static Future<List<Post>> getPosts() async {
|
||||
var url = Uri.parse("https://jsonplaceholder.typicode.com/albums/1/photos");
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
var accessToken = prefs.getString("access_token") ?? "";
|
||||
final List<Post> 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();
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
// build function
|
||||
@override
|
||||
@ -84,7 +92,6 @@ class _MyHomePageState extends State<ListItemMenu> {
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(flex: 1, child: Image.network(post.url!)),
|
||||
SizedBox(width: 10),
|
||||
Expanded(flex: 3, child: Text(post.title!)),
|
||||
],
|
||||
),
|
||||
|
Loading…
x
Reference in New Issue
Block a user