|
| 1 | +--- |
| 2 | +title: Quickstart |
| 3 | +description: Minimal setup and first calls (search, mutate, actions). |
| 4 | +--- |
| 5 | + |
| 6 | +# Quickstart |
| 7 | + |
| 8 | +## 1) Configure Dio + SDK client |
| 9 | + |
| 10 | +```dart |
| 11 | +import 'package:dio/dio.dart'; |
| 12 | +import 'package:laravel_rest_api_flutter/laravel_rest_api_flutter.dart'; |
| 13 | +
|
| 14 | +final dio = Dio( |
| 15 | + BaseOptions( |
| 16 | + baseUrl: 'https://api.example.com', |
| 17 | + headers: { |
| 18 | + 'Accept': 'application/json', |
| 19 | + 'Authorization': 'Bearer YOUR_TOKEN', |
| 20 | + }, |
| 21 | + ), |
| 22 | +); |
| 23 | +
|
| 24 | +final client = ApiHttpClient(dio: dio); |
| 25 | +``` |
| 26 | + |
| 27 | +## 2) Create a repository |
| 28 | + |
| 29 | +```dart |
| 30 | +class ItemsRepository |
| 31 | + with |
| 32 | + SearchFactory<Map<String, dynamic>>, |
| 33 | + DeleteFactory<Map<String, dynamic>>, |
| 34 | + MutateFactory, |
| 35 | + ActionsFactory { |
| 36 | + final RestApiClient client; |
| 37 | +
|
| 38 | + ItemsRepository(this.client); |
| 39 | +
|
| 40 | + @override |
| 41 | + String get baseRoute => '/items'; |
| 42 | +
|
| 43 | + @override |
| 44 | + RestApiClient get httpClient => client; |
| 45 | +
|
| 46 | + @override |
| 47 | + Map<String, dynamic> fromJson(Map<String, dynamic> json) => json; |
| 48 | +
|
| 49 | + @override |
| 50 | + void onCatchError( |
| 51 | + RestApiResponse? response, |
| 52 | + Object exception, |
| 53 | + StackTrace stacktrace, |
| 54 | + ) {} |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +## 3) Call endpoints |
| 59 | + |
| 60 | +### Search |
| 61 | + |
| 62 | +```dart |
| 63 | +final repo = ItemsRepository(client); |
| 64 | +
|
| 65 | +final res = await repo.search( |
| 66 | + text: TextSearch(value: 'hammer'), |
| 67 | + page: 1, |
| 68 | + limit: 10, |
| 69 | + sorts: [Sort(field: 'created_at', direction: 'desc')], |
| 70 | +); |
| 71 | +
|
| 72 | +print(res.data); // List<Map<String, dynamic>> |
| 73 | +``` |
| 74 | + |
| 75 | +### Mutate |
| 76 | + |
| 77 | +```dart |
| 78 | +final res = await repo.mutate( |
| 79 | + body: LaravelRestApiMutateBody( |
| 80 | + mutate: [ |
| 81 | + Mutation( |
| 82 | + operation: MutationOperation.create, |
| 83 | + attributes: {'name': 'New item'}, |
| 84 | + ), |
| 85 | + ], |
| 86 | + ), |
| 87 | +); |
| 88 | +
|
| 89 | +print(res.data?.created); |
| 90 | +``` |
| 91 | + |
| 92 | +### Actions |
| 93 | + |
| 94 | +```dart |
| 95 | +final res = await repo.actions( |
| 96 | + actionUriKey: 'publish', |
| 97 | + data: LaravelRestApiActionsBody( |
| 98 | + fields: [ |
| 99 | + Action(name: 'published_at', value: DateTime.now().toIso8601String()), |
| 100 | + ], |
| 101 | + ), |
| 102 | +); |
| 103 | +
|
| 104 | +print(res.data); // impacted count |
| 105 | +``` |
0 commit comments