This repository has been archived on 2026-06-02. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
FabledApp/lib/data/api/projects_api.dart
T
bvandeusen c8becd6afd fix: revert null-aware map entries; disable use_null_aware_elements lint
The ? operator on map entries applies to the key, not the value.
String literal keys can never be null, so ?'key': value was flagged as
invalid_null_aware_operator. Reverted to if (x != null) 'key': x form
and disabled the lint project-wide since it doesn't apply here.
2026-03-12 00:44:04 -04:00

72 lines
1.9 KiB
Dart

import 'package:dio/dio.dart';
import '../models/project.dart';
import 'api_client.dart';
class ProjectsApi {
final Dio _dio;
const ProjectsApi(this._dio);
Future<List<Project>> getAll({String? status}) async {
try {
final response = await _dio.get(
'/api/projects',
queryParameters: status != null ? {'status': status} : null,
);
final data = response.data as Map<String, dynamic>;
final list = data['projects'] as List<dynamic>;
return list
.map((e) => Project.fromJson(e as Map<String, dynamic>))
.toList();
} on DioException catch (e) {
throw dioToApp(e);
}
}
Future<Project> getOne(int id) async {
try {
final response = await _dio.get('/api/projects/$id');
return Project.fromJson(response.data as Map<String, dynamic>);
} on DioException catch (e) {
throw dioToApp(e);
}
}
Future<Project> create({
required String title,
String? description,
String? goal,
String? color,
}) async {
try {
final response = await _dio.post('/api/projects', data: {
'title': title,
if (description != null && description.isNotEmpty)
'description': description,
if (goal != null && goal.isNotEmpty) 'goal': goal,
if (color != null) 'color': color,
});
return Project.fromJson(response.data as Map<String, dynamic>);
} on DioException catch (e) {
throw dioToApp(e);
}
}
Future<Project> update(int id, Map<String, dynamic> fields) async {
try {
final response = await _dio.patch('/api/projects/$id', data: fields);
return Project.fromJson(response.data as Map<String, dynamic>);
} on DioException catch (e) {
throw dioToApp(e);
}
}
Future<void> delete(int id) async {
try {
await _dio.delete('/api/projects/$id');
} on DioException catch (e) {
throw dioToApp(e);
}
}
}