در این مثال api من لیستی از post رو دریافت میکرد و به این صورت نمایش میداد :
[
{
"id": 1,
"title": "Dolores molestiae",
"content": "Dolores molestiae modi laboriosam ipsum voluptate in a. Quis omnis nisi tenetur. Odio et nihil provident vitae dolorem eos sapiente.",
"photo": "https://picsum.photos/200",
"user_id": 1,
"active": 1,
"created_at": null,
"updated_at": null,
"user": {
"id": 1,
"name": "John Doe",
"email": "2cw5FejUZp@gmail.com",
"photo": "public/photo/user.png",
"active": 1,
"created_at": "2020-03-28T07:00:00.000000Z",
"updated_at": null
}
},
service.dart
import 'dart:convert';
import 'package:eshop/services/models/post.dart';
import 'package:http/http.dart' as http;
class PostLoader {
Future<List<Post>> fetchPosts() async {
try {
final response =
await http.get('http://kashune.com/flutter/api/v1/posts');
if (response.statusCode == 200) {
List<Post> posts = (json.decode(response.body) as List)
.map((i) => Post.fromJson(i))
.toList();
// posts.forEach((element) => print(element.id));
return posts;
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
throw Exception('FUCKED UP');
}
} catch (e) {
print('Fucking error is $e');
return null;
}
}
}
post.dart // Post model
class Post {
final int id;
final String title;
final String content;
final String photo;
Post({this.id, this.title, this.content, this.photo});
//convert json to model
factory Post.fromJson(Map<String, dynamic> json) {
return Post(
id: json['id'],
title: json['title'],
content: json['content'],
photo: json['photo'],
);
}
//convert model to json
Map<String, dynamic> toJson() => {
"id": id,
"title": title,
"content": content,
"photo": photo
};
}
main.dart
import 'package:eshop/services/models/post.dart';
import 'package:eshop/services/service.dart';
import 'package:flutter/material.dart';
void main() => runApp(PostApp());
class PostApp extends StatefulWidget {
@override
_PostAppState createState() => _PostAppState();
}
class _PostAppState extends State<PostApp> {
List<Post> posts=[]; //init empty to avoid null error at
void loader() async {
PostLoader instance=PostLoader();
Future<List<Post>> object = instance.fetchPosts();
object.then((List<Post> _posts) {
// posts.forEach((element) => print(element.title));
setState(() {
posts = _posts;
});
});
}
@override
void initState() {
loader();
super.initState();
}
Widget postTemplate(post) {
return Card(
child: ListTile(
leading: CircleAvatar(
backgroundImage: NetworkImage(post.photo),
),
title: Text(post.title),
subtitle: Text(post.content.substring(0,50)+' .'),
trailing: Icon(Icons.more_vert),
),
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Posts List',
home: Scaffold(
appBar: AppBar(
title: Text('Post List'),
),
body: ListView.builder(
itemCount: posts.length,
itemBuilder: (context, index) {
final item = posts[index];
return postTemplate(item);
},
),
),
);
}
}
دسته بندی با بی تهایت زیردسته بندی
حذف از کالکشن با شرط در لاراول - Remove from a collection on a condition in Laravel
,post ,dart ,id ,title , , ,1 ,import package ,dart import ,eshop services ,dart import package ,package eshop services , posts foreach element ,eshop services models
درباره این سایت