Skip to content

Instantly share code, notes, and snippets.

@sorgfal
Last active November 17, 2023 13:59
Show Gist options
  • Select an option

  • Save sorgfal/f349472ba7804a02dfb18ad3fb1fa548 to your computer and use it in GitHub Desktop.

Select an option

Save sorgfal/f349472ba7804a02dfb18ad3fb1fa548 to your computer and use it in GitHub Desktop.
amber-aqueduct-1548
import 'package:http/http.dart' as http;
import 'dart:convert';
void main() async {
final todo = await getTodoById(id: 1).then((data) {
/// Парсинг в дто
return data;
}).onError((e, s) => null);
print(todo);
}
Future<
({
int userId,
int id,
String title,
bool completed,
})?> getTodoById({required int id}) async {
final responseData = await http
.get(Uri.parse('https://jsonplaceholder.typicode.com/todos/$id'))
.then((resp) {
return jsonDecode(resp.body);
});
return (
userId: responseData['userId'] as int,
id: responseData['id'] as int,
title: responseData['title'] as String,
completed: responseData['completed'] as bool,
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment