feature/itemlist #2
@ -2,6 +2,7 @@ import 'dart:convert';
|
|||||||
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/post.dart';
|
||||||
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
// app starting point
|
// app starting point
|
||||||
void main() {
|
void main() {
|
||||||
@ -35,11 +36,18 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|||||||
|
|
||||||
// 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<Post>> getPosts() async {
|
||||||
var url = Uri.parse("https://jsonplaceholder.typicode.com/albums/1/photos");
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||||
final response =
|
var accessToken = prefs.getString("access_token") ?? "";
|
||||||
await http.get(url, headers: {"Content-Type": "application/json"});
|
final List<Post> body = [];
|
||||||
final List body = json.decode(response.body);
|
if (accessToken.isNotEmpty) {
|
||||||
return body.map((e) => Post.fromJson(e)).toList();
|
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
|
// build function
|
||||||
@ -84,7 +92,6 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|||||||
child: Row(
|
child: Row(
|
||||||
children: [
|
children: [
|
||||||
Expanded(flex: 1, child: Image.network(post.url!)),
|
Expanded(flex: 1, child: Image.network(post.url!)),
|
||||||
SizedBox(width: 10),
|
|
||||||
Expanded(flex: 3, child: Text(post.title!)),
|
Expanded(flex: 3, child: Text(post.title!)),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user