0% found this document useful (0 votes)
21 views2 pages

Requetes API Flutter Sqflite

The document outlines a Flutter application that manages posts using an API and a local database with Sqflite. It includes CRUD operations for creating, reading, updating, and deleting posts, along with the implementation of a Post class and a DatabaseHelper class for database interactions. The API endpoints are specified for fetching and modifying posts from a remote server.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

Requetes API Flutter Sqflite

The document outlines a Flutter application that manages posts using an API and a local database with Sqflite. It includes CRUD operations for creating, reading, updating, and deleting posts, along with the implementation of a Post class and a DatabaseHelper class for database interactions. The API endpoints are specified for fetching and modifying posts from a remote server.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PUT (Modifier un Post

Requêtes API en existant)


Gestion de base
Flutter Future<Post ?>
de données locale
updatePost(Post post) async
{
avec Sqflite
GET (Liste de Posts) final url =
[Link]('[Link]
Future<List<Post>> [Link]/posts/$ 2. Création de la classe Post
fetchPosts() async { {[Link]}');
final url = class Post {
[Link]('[Link] final response = await final int id;
[Link]/posts'); [Link]( final String title;
final response = await url, final String body;
[Link](url); headers: {'Content-
Type': 'application/json'}, Post({required [Link],
if ([Link] == body: required [Link],
200) { [Link]([Link]()), required [Link]});
final List<dynamic> );
jsonList = Map<String, dynamic>
[Link]([Link]); if ([Link] == toMap() {
return 200) { return {
[Link]((json) => final jsonData = 'id': id,
[Link](json)).toList( [Link]([Link]); 'title': title,
); return 'body': body,
} else { [Link](jsonData); };
throw Exception('Erreur } else { }
lors du chargement des return null;
posts'); } factory
} } [Link](Map<String,
} dynamic> map) {
return Post(
id: map['id'],
POST (Créer un nouveau Post) DELETE (Supprimer un Post) title: map['title'],
body: map['body'],
);
Future<Post?> Future<bool> deletePost(int
}
createPost(Post post) async id) async {
}
{ final url =
final url = [Link]('[Link]
[Link]('[Link] [Link]/posts/$i
[Link]/posts'); d');
final response = await
final response = await [Link](url);
[Link](
url, if ([Link] ==
headers: {'Content- 200) {
Type': 'application/json'}, print("Le post a été
body: supprimé");
[Link]([Link]()), return true;
); } else {
print("Erreur lors de la
if ([Link] == suppression");
201) { return false;
final jsonData = }
[Link]([Link]); }
return
[Link](jsonData);
} else {return null}
3. Création de la base de 4. Fonctions CRUD (Create,
données Read, Update, Delete)

import
Create
'package:sqflite/[Link]
Future<void> insertPost(Post
t';
post) async {
import
final db = await
'package:path/[Link]';
[Link]
base;
class DatabaseHelper {
await [Link]('posts',
static final
[Link](),
DatabaseHelper instance =
conflictAlgorithm:
DatabaseHelper._init();
[Link]);
static Database?
}
_database;

DatabaseHelper._init();
Read
Future<Database> get Future<List<Post>>
database async { getPosts() async {
if (_database != null) final db = await
return _database!; [Link]
_database = await base;
_initDB('[Link]'); final maps = await
return _database!; [Link]('posts');
} return [Link]((map) =>
[Link](map)).toList();
Future<Database> }
_initDB(String filePath)
async { Update
final dbPath = await Future<void> updatePost(Post
getDatabasesPath(); post) async {
final path = final db = await
join(dbPath, filePath); [Link]
base;
return await await [Link]('posts',
openDatabase(path, version: [Link](), where: 'id
1, onCreate: _createDB); = ?', whereArgs: [[Link]]);
} }

Future _createDB(Database
db, int version) async {
await [Link](''' Delete
CREATE TABLE posts ( Future<void> deletePost(int
id INTEGER PRIMARY id) async {
KEY, final db = await
title TEXT NOT NULL, [Link]
body TEXT NOT NULL base;
) await [Link]('posts',
'''); where: 'id = ?', whereArgs:
} [id]);
} }

You might also like