docs(rest_api): add example for REST Api handling
Related Issues
related to: #3932
Add example for handling REST API with riverpod.
- Fetch users (GET)
- Create user (POST)
- Loading/error states
- Pull-to-refresh
- Testing
Please let me know when there is anything to improve :) I'll do it asap :muscle: :rocket:
Checklist
Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]).
-
[ ] I have updated the
CHANGELOG.mdof the relevant packages. Changelog files must be edited under the form:## Unreleased fix/major/minor - Description of your change. (thanks to @yourGithubId) -
[ ] If this contains new features or behavior changes, I have updated the documentation to match those changes.
Summary by CodeRabbit
-
New Features
- Added a REST API example project using Riverpod for state management.
- Implemented user management functionality with CRUD operations.
- Created screens for listing and adding users.
- Introduced a metadata file for project tracking and configuration.
-
Documentation
- Added project README with detailed explanation of functionality.
- Created analysis configuration for code quality.
-
Testing
- Implemented unit tests for repository and state management.
- Added widget tests for UI components and error scenarios.
Walkthrough
This pull request introduces a comprehensive example of a REST API Flutter application using Riverpod for state management and Dio for HTTP requests. The project includes a complete implementation with a user management interface, featuring user listing, adding new users, error handling, and a full test suite. The example demonstrates modern Dart and Flutter development practices, including asynchronous state management, form validation, and comprehensive testing strategies.
Changes
| File | Change Summary |
|---|---|
examples/rest_api/.metadata |
New project metadata file tracking Flutter SDK version and project configuration |
examples/rest_api/README.md |
New documentation explaining the REST API example's functionality and structure |
examples/rest_api/analysis_options.yaml |
Added linting and static analysis configuration for code quality |
examples/rest_api/lib/main.dart |
Implemented core application logic with User model, repository, state management, and UI components |
examples/rest_api/pubspec.yaml |
Defined project dependencies, including Riverpod, Dio, and testing libraries |
examples/rest_api/test/user_test.dart |
Added unit tests for UserRepository and UsersNotifier |
examples/rest_api/test/user_test.mocks.dart |
Generated mock classes for Dio testing |
examples/rest_api/test/widget_test.dart |
Implemented widget tests for user list and add user screens |
Sequence Diagram
sequenceDiagram
participant UI as UserListScreen
participant State as UsersNotifier
participant Repo as UserRepository
participant API as REST API
UI->>State: Fetch Users
State->>Repo: fetchUsers()
Repo->>API: GET /users
API-->>Repo: Return User List
Repo-->>State: Update Users
State-->>UI: Render User List
UI->>State: Add New User
State->>Repo: createUser(newUser)
Repo->>API: POST /users
API-->>Repo: Confirm User Creation
Repo-->>State: Update State
State-->>UI: Refresh User List
Poem
🐰 A Rabbit's REST API Tale 🌐
In Flutter's realm, with Riverpod's might,
Users dance through requests so bright.
Dio fetches, state transforms with glee,
A code adventure, wild and free!
Hop, hop, API magic unfurled! 🚀
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?
🪧 Tips
Chat
There are 3 ways to chat with CodeRabbit:
- Review comments: Directly reply to a review comment made by CodeRabbit. Example:
I pushed a fix in commit <commit_id>, please review it.Generate unit testing code for this file.Open a follow-up GitHub issue for this discussion.
- Files and specific lines of code (under the "Files changed" tab): Tag
@coderabbitaiin a new review comment at the desired location with your query. Examples:@coderabbitai generate unit testing code for this file.@coderabbitai modularize this function.
- PR comments: Tag
@coderabbitaiin a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.@coderabbitai read src/utils.ts and generate unit testing code.@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.@coderabbitai help me debug CodeRabbit configuration file.
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.
CodeRabbit Commands (Invoked using PR comments)
@coderabbitai pauseto pause the reviews on a PR.@coderabbitai resumeto resume the paused reviews.@coderabbitai reviewto trigger an incremental review. This is useful when automatic reviews are disabled for the repository.@coderabbitai full reviewto do a full review from scratch and review all the files again.@coderabbitai summaryto regenerate the summary of the PR.@coderabbitai generate docstringsto generate docstrings for this PR. (Beta)@coderabbitai resolveresolve all the CodeRabbit review comments.@coderabbitai configurationto show the current CodeRabbit configuration for the repository.@coderabbitai helpto get help.
Other keywords and placeholders
- Add
@coderabbitai ignoreanywhere in the PR description to prevent this PR from being reviewed. - Add
@coderabbitai summaryto generate the high-level summary at a specific location in the PR description. - Add
@coderabbitaianywhere in the PR title to generate the title automatically.
CodeRabbit Configuration File (.coderabbit.yaml)
- You can programmatically configure CodeRabbit by adding a
.coderabbit.yamlfile to the root of your repository. - Please see the configuration documentation for more information.
- If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation:
# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json
Documentation and Community
- Visit our Documentation for detailed information on how to use CodeRabbit.
- Join our Discord Community to get help, request features, and share feedback.
- Follow us on X/Twitter for updates and announcements.
The changes proposed by Code Rabbit are really cool, but aren't some of them a bit too advanced for a beginner example?
Also the failing code-generation check I don't fully understand yet. But I'll investigate it further asap :)
@rrousselGit it seems that the workflow failed due to a problem with the dart file. Can you rerun the jobs?
Run flutter pub get
flutter pub get
shell: /usr/bin/bash -e {0}
env:
FLUTTER_ROOT: /opt/hostedtoolcache/flutter/master-master-x64
PUB_CACHE: /home/runner/.pub-cache
Downloading Linux x64 Dart SDK from Flutter engine cdce7658473f4eafbf9d[2](https://github.com/rrousselGit/riverpod/actions/runs/13287118192/job/37098382308#step:6:2)83affd3de6d73fd0d97...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 255 100 255 0 0 28[3](https://github.com/rrousselGit/riverpod/actions/runs/13287118192/job/37098382308#step:6:3)0 0 --:--:-- --:--:-- --:--:-- 2833
[/opt/hostedtoolcache/flutter/master-master-x6[4](https://github.com/rrousselGit/riverpod/actions/runs/13287118192/job/37098382308#step:6:4)/bin/cache/dart-sdk-linux-x64.zip]
End-of-central-directory signature not found. Either this file is not
a zipfile, or it constitutes one disk of a multi-part archive. In the
latter case the central directory and zipfile comment will be found on
the last disk(s) of this archive.
unzip: cannot find zipfile directory in one of /opt/hostedtoolcache/flutter/master-master-x[6](https://github.com/rrousselGit/riverpod/actions/runs/13287118192/job/37098382308#step:6:6)4/bin/cache/dart-sdk-linux-x64.zip or
/opt/hostedtoolcache/flutter/master-master-x64/bin/cache/dart-sdk-linux-x64.zip.zip, and cannot find /opt/hostedtoolcache/flutter/master-master-x64/bin/cache/dart-sdk-linux-x64.zip.ZIP, period.
It appears that the downloaded file is corrupt; please try again.
If this problem persists, please report the problem at:
https://github.com/flutter/flutter/issues/new?template=1_activation.yml