### Updating a User Model Source: https://github.com/flutterdata/flutter_data.git/blob/master/README.md This example demonstrates how to update a 'User' model using Flutter Data's ActiveRecord-style extension methods. By calling `.save()` on a `User` instance, the framework handles the persistence and synchronization of the updated data. This method is a convenient way to modify model states. ```dart TextButton( onPressed: () => ref.users.save(User(id: 1, name: 'Updated')), child: Text('Update'), ), // ActiveRecord-style update: GestureDetector( onTap: () => User(id: 1, name: 'Updated').save(), child: Text('Update') ), ``` -------------------------------- ### Watching a User Model via Riverpod Source: https://github.com/flutterdata/flutter_data.git/blob/master/README.md This code snippet shows how to access and watch a specific 'User' model using Riverpod's `ref`. It retrieves the `Adapter` generated by Flutter Data and uses `watchOne(1)` to get the state of the user with ID 1. The UI conditionally displays a loading indicator or the user's name based on the `DataState` (isLoading, model). ```dart @override Widget build(BuildContext context, WidgetRef ref) { final state = ref.users.watchOne(1); if (state.isLoading) { return Center(child: const CircularProgressIndicator()); } final user = state.model; return Text(user.name); } ``` -------------------------------- ### Flutter Data Initialization with Riverpod Source: https://github.com/flutterdata/flutter_data.git/blob/master/README.md Demonstrates how to initialize Flutter Data by providing a local storage instance through Riverpod's overrides. It shows the configuration of LocalStorage with a base directory function and busy timeout, and how to watch the `initializeFlutterData` future. ```dart ProviderScope( overrides: [ localStorageProvider.overrideWithValue( LocalStorage( baseDirFn: () async { return (await getApplicationSupportDirectory()).path; }, busyTimeout: 5000, clear: LocalStorageClearStrategy.never, ), ) ], // ... ), ``` ```dart return Scaffold( body: ref.watch(initializeFlutterData(adapterProvidersMap)).when( data: (_) => child, error: (e, _) => const Text('Error'), loading: () => const Center(child: CircularProgressIndicator()), ), ``` -------------------------------- ### Running Tests Source: https://github.com/flutterdata/flutter_data.git/blob/master/README.md Instructions on how to execute the project's tests using the Dart testing framework. ```dart dart test ``` -------------------------------- ### Flutter Data Model and Adapter Concepts Source: https://github.com/flutterdata/flutter_data.git/blob/master/README.md This section explains the core concepts of Flutter Data, including how models are defined with `@DataAdapter` and how custom adapters (like `MyJSONServerAdapter`) extend default behavior. It highlights the role of code generation in creating `Adapter` instances accessible via Riverpod providers (e.g., `ref.users`). The reactive nature of data fetching and local changes is also emphasized. ```APIDOC Flutter Data Core Concepts: Model Definition: - Models are Dart classes representing data structures. - Annotate models with `@JsonSerializable()` for JSON serialization. - Use `@DataAdapter([CustomAdapter])` to specify custom data handling. Custom Adapters: - Mixins that extend `RemoteAdapter` or `LocalAdapter`. - Define behavior for CRUD operations and remote endpoints. - Example: `MyJSONServerAdapter` sets `baseUrl` for remote requests. Code Generation: - Flutter Data generates `Adapter` classes based on model definitions. - These adapters provide methods for data manipulation (save, delete, find, etc.). Riverpod Integration: - Generated adapters are exposed as Riverpod providers (e.g., `ref.users` for `Adapter`). - Enables reactive data watching using methods like `watchOne()`. DataState: - Represents the state of data operations (loading, error, data). - Used to build UI components that react to data changes. ActiveRecord Pattern: - Models can have extension methods like `.save()`, `.delete()` for direct manipulation. - Simplifies common data operations. Example Workflow: 1. Define a model (e.g., `User`). 2. Create a custom adapter mixin (e.g., `MyJSONServerAdapter`). 3. Annotate the model with `@DataAdapter`. 4. Run code generation. 5. Access the adapter via Riverpod (`ref.users`). 6. Watch or modify model instances (e.g., `ref.users.watchOne(1)`, `user.save()`). ``` -------------------------------- ### Flutter Data Local Storage Adapter API Source: https://github.com/flutterdata/flutter_data.git/blob/master/README.md Provides the public API methods for interacting with local storage adapters in Flutter Data. These methods cover finding, saving, deleting, and managing data models, as well as querying the state of the local storage. ```APIDOC Local Storage Adapter Methods: findAllLocal(): List - Retrieves all models of a specific type from local storage. findManyLocal(Iterable keys): List - Retrieves multiple models from local storage using their keys. findOneLocal(String? key): T? - Retrieves a single model from local storage by its key. findOneLocalById(Object id): T? - Retrieves a single model from local storage by its ID. exists(String key): bool - Checks if a model with the given key exists in local storage. saveLocal(T model, {bool notify = true}): T - Saves a single model to local storage. Optionally notifies listeners. saveManyLocal(Iterable models, {bool notify = true, bool async = true}): Future? > - Saves multiple models to local storage. Supports asynchronous saving and notification. deleteLocal(T model, {bool notify = true}): void - Deletes a model from local storage. Optionally notifies listeners. deleteLocalById(Object id, {bool notify = true}): void - Deletes a model from local storage by its ID. Optionally notifies listeners. deleteLocalByKeys(Iterable keys, {bool notify = true}): void - Deletes multiple models from local storage by their keys. Optionally notifies listeners. clearLocal({bool notify = true}): Future - Clears all data from local storage. Optionally notifies listeners. countLocal: int - Returns the total number of models stored locally. keys: Set - Returns a set of all keys present in local storage. ``` -------------------------------- ### Flutter Data Watchers Source: https://github.com/flutterdata/flutter_data.git/blob/master/README.md Enables real-time data watching and state management. Provides methods to observe changes in collections or individual models, with options for remote or local data sources and background synchronization. ```APIDOC watchAll({bool remote = false, Map? params, Map? headers, bool syncLocal = false, String? finder, DataRequestLabel? label}) - Returns a DataState> for observing all models. watchOne(Object model, {bool remote = false, Map? params, Map? headers, AlsoWatch? alsoWatch, String? finder, DataRequestLabel? label}) - Returns a DataState for observing a single model. watchAllNotifier({bool remote = false, Map? params, Map? headers, bool syncLocal = false, String? finder, DataRequestLabel? label}) - Returns a DataStateNotifier> for observing all models with notifier. watchOneNotifier(Object model, {bool remote = false, Map? params, Map? headers, AlsoWatch? alsoWatch, String? finder, DataRequestLabel? label}) - Returns a DataStateNotifier for observing a single model with notifier. final coreNotifierThrottleDurationProvider; - Provider for the throttle duration of notifiers. ``` -------------------------------- ### Flutter Data Serialization Source: https://github.com/flutterdata/flutter_data.git/blob/master/README.md Handles the conversion of models to and from data formats like Maps. Supports asynchronous operations and relationship management during serialization and deserialization. ```APIDOC serialize(T model, {bool withRelationships = true}) - Serializes a model to a Map. deserialize(Object? data, {String? key, bool async = true}) - Deserializes data into a model. deserializeAndSave(Object? data, {String? key, bool notify = true, bool ignoreReturn = false}) - Deserializes data and saves it. ``` -------------------------------- ### Defining a User Model with a Custom Adapter Source: https://github.com/flutterdata/flutter_data.git/blob/master/README.md This snippet demonstrates how to define a Flutter Data model named 'User' and associate it with a custom JSON server adapter. The `@DataAdapter` annotation specifies the adapter to use, and the `MyJSONServerAdapter` mixin defines the base URL for remote data operations. The model extends `DataModel` and includes an optional `id` and a required `name` field. Code generation is required for this to work. ```dart @JsonSerializable() @DataAdapter([MyJSONServerAdapter]) class User extends DataModel { @override final int? id; // ID can be of any type final String name; User({this.id, required this.name}); // `User.fromJson` and `toJson` optional } mixin MyJSONServerAdapter on RemoteAdapter { @override String get baseUrl => "https://my-json-server.typicode.com/flutterdata/demo/"; } ``` -------------------------------- ### Flutter Data Remote Operations Source: https://github.com/flutterdata/flutter_data.git/blob/master/README.md Provides methods for performing CRUD operations on remote data sources. Supports background operations, custom parameters, headers, and callbacks for success and error. ```APIDOC findAll({bool remote = true, bool background = false, Map? params, Map? headers, bool syncLocal = false, OnSuccessAll? onSuccess, OnErrorAll? onError, DataRequestLabel? label}) - Fetches all models of type T. findOne(Object id, {bool remote = true, bool background = false, Map? params, Map? headers, OnSuccessOne? onSuccess, OnErrorOne? onError, DataRequestLabel? label}) - Fetches a single model of type T by its ID. save(T model, {bool remote = true, Map? params, Map? headers, OnSuccessOne? onSuccess, OnErrorOne? onError, DataRequestLabel? label}) - Saves a model of type T. Creates or updates the model on the remote source. delete(Object model, {bool remote = true, Map? params, Map? headers, OnSuccessOne? onSuccess, OnErrorOne? onError, DataRequestLabel? label}) - Deletes a model of type T. Set> get offlineOperations; - Returns a set of offline operations available for the model. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.