1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-08 13:58:47 +00:00
Files
samples/code_sharing
dependabot[bot] 7f2e0d93fd Bump lints from 2.1.1 to 3.0.0 in /code_sharing/shared (#2064)
Bumps [lints](https://github.com/dart-lang/lints) from 2.1.1 to 3.0.0.
<details>
<summary>Release notes</summary>
<p><em>Sourced from <a href="https://github.com/dart-lang/lints/releases">lints's releases</a>.</em></p>
<blockquote>
<h2>package:lints v3.0.0</h2>
<ul>
<li><code>core</code>:
<ul>
<li>added <code>collection_methods_unrelated_type</code></li>
<li>added <code>dangling_library_doc_comments</code></li>
<li>added <code>implicit_call_tearoffs</code></li>
<li>added <code>secure_pubspec_urls</code></li>
<li>added <code>type_literal_in_constant_pattern</code></li>
<li>added <code>use_string_in_part_of_directives</code></li>
<li>removed <code>iterable_contains_unrelated_type</code></li>
<li>removed <code>list_remove_unrelated_type</code></li>
<li>removed <code>no_wildcard_variable_uses</code></li>
</ul>
</li>
<li><code>recommended</code>:
<ul>
<li>added <code>unnecessary_to_list_in_spreads</code></li>
<li>added <code>use_super_parameters</code></li>
<li>removed <code>prefer_equal_for_default_values</code></li>
<li>removed <code>prefer_void_to_null</code></li>
</ul>
</li>
<li>Add info about which lints have quick fixes to the package's readme.</li>
<li>Move the list of lint rules from the readme to a separate
<a href="https://github.com/dart-lang/lints/blob/main/rules.md">rules.md</a> file.</li>
</ul>
<h2>package:lints v3.0.0-beta.2</h2>
<ul>
<li>Add <code>dangling_library_doc_comments</code> to core.</li>
<li>Remove <code>no_wildcard_variable_uses</code> from core.</li>
<li>Remove <code>prefer_void_to_null</code> from recommended.</li>
</ul>
<h2>package:lints v3.0.0-beta</h2>
<ul>
<li><code>core</code>:
<ul>
<li>added <code>collection_methods_unrelated_type</code></li>
<li>added <code>implicit_call_tearoffs</code></li>
<li>added <code>no_wildcard_variable_uses</code></li>
<li>added <code>secure_pubspec_urls</code></li>
<li>added <code>type_literal_in_constant_pattern</code></li>
<li>added <code>use_string_in_part_of_directives</code></li>
<li>removed <code>iterable_contains_unrelated_type</code></li>
<li>removed <code>list_remove_unrelated_type</code></li>
</ul>
</li>
<li><code>recommended</code>:
<ul>
<li>added <code>unnecessary_to_list_in_spreads</code></li>
<li>added <code>use_super_parameters</code></li>
<li>removed <code>prefer_equal_for_default_values</code></li>
</ul>
</li>
<li>Add info about which lints have quick fixes to the package's readme.</li>
<li>Move the list of lint rules from the readme to a separate
<a href="https://github.com/dart-lang/lints/blob/main/rules.md">rules.md</a> file.</li>
</ul>
</blockquote>
</details>
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a href="https://github.com/dart-lang/lints/blob/main/CHANGELOG.md">lints's changelog</a>.</em></p>
<blockquote>
<h2>3.0.0</h2>
<ul>
<li><code>core</code>:
<ul>
<li>added <code>collection_methods_unrelated_type</code></li>
<li>added <code>dangling_library_doc_comments</code></li>
<li>added <code>implicit_call_tearoffs</code></li>
<li>added <code>secure_pubspec_urls</code></li>
<li>added <code>type_literal_in_constant_pattern</code></li>
<li>added <code>use_string_in_part_of_directives</code></li>
<li>removed <code>iterable_contains_unrelated_type</code></li>
<li>removed <code>list_remove_unrelated_type</code></li>
<li>removed <code>no_wildcard_variable_uses</code></li>
</ul>
</li>
<li><code>recommended</code>:
<ul>
<li>added <code>unnecessary_to_list_in_spreads</code></li>
<li>added <code>use_super_parameters</code></li>
<li>removed <code>prefer_equal_for_default_values</code></li>
<li>removed <code>prefer_void_to_null</code></li>
</ul>
</li>
<li>Add info about which lints have quick fixes to the package's readme.</li>
<li>Move the list of lint rules from the readme to a separate
<a href="https://github.com/dart-lang/lints/blob/main/rules.md">rules.md</a> file.</li>
</ul>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a href="975c687068"><code>975c687</code></a> rev to 3.0.0; prep to publish (<a href="https://redirect.github.com/dart-lang/lints/issues/160">#160</a>)</li>
<li><a href="90d350eb89"><code>90d350e</code></a> Bump actions/checkout from 3.6.0 to 4.1.0 (<a href="https://redirect.github.com/dart-lang/lints/issues/159">#159</a>)</li>
<li><a href="140c8025fe"><code>140c802</code></a> Bump dart-lang/setup-dart from 1.5.0 to 1.5.1 (<a href="https://redirect.github.com/dart-lang/lints/issues/158">#158</a>)</li>
<li><a href="624a09da9c"><code>624a09d</code></a> add dangling_library_doc_comments, remove prefer_void_to_null (<a href="https://redirect.github.com/dart-lang/lints/issues/157">#157</a>)</li>
<li><a href="296efaf291"><code>296efaf</code></a> remove no_wildcard_variable_uses; improve testing (<a href="https://redirect.github.com/dart-lang/lints/issues/156">#156</a>)</li>
<li><a href="c266a04d9b"><code>c266a04</code></a> rev to 3.0.0-beta in preparation for publishing (<a href="https://redirect.github.com/dart-lang/lints/issues/153">#153</a>)</li>
<li><a href="b044acab9f"><code>b044aca</code></a> add several rules to core and recommended (<a href="https://redirect.github.com/dart-lang/lints/issues/150">#150</a>)</li>
<li><a href="81100a2eba"><code>81100a2</code></a> fix a dangling table link (<a href="https://redirect.github.com/dart-lang/lints/issues/146">#146</a>)</li>
<li><a href="8d5f750002"><code>8d5f750</code></a> move the list of lint rules from the readme to a separate rules.md file (<a href="https://redirect.github.com/dart-lang/lints/issues/145">#145</a>)</li>
<li><a href="da44af3d26"><code>da44af3</code></a> Bump actions/checkout from 3.5.3 to 3.6.0 (<a href="https://redirect.github.com/dart-lang/lints/issues/144">#144</a>)</li>
<li>Additional commits viewable in <a href="https://github.com/dart-lang/lints/compare/v2.1.1...v3.0.0">compare view</a></li>
</ul>
</details>
<br />

[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=lints&package-manager=pub&previous-version=2.1.1&new-version=3.0.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)

</details>
2023-10-19 07:07:24 +00:00
..
2023-08-24 11:52:33 -05:00
2023-08-24 11:52:33 -05:00

Code Sharing

A full-stack Dart application using Flutter on the client and shelf on the server. The Flutter app itself is still the counter app, but the actual number is stored on the server and incremented over HTTP using transport data classes understood by both the Flutter client and shelf server.

Goals

The goal of this sample is to demonstrate the mechanics of sharing business logic between a Flutter client and a Dart server. The sample also includes a slightly modified Dockerfile which is required to build a Docker image from a Dart app containing nested packages.

Project Structure

The sample's project structure is as follows:

code_sharing/
  # Flutter app
  client/
    lib/
      ...
    pubspec.yaml
    ...
  # Shelf
  server/
    bin/
      server.dart
    pubspec.yaml
    Dockerfile
  # Common business logic
  shared/
    pubspec.yaml
    lib/
      ...

Recreating this on your own

Recreating this introductory project for yourself can be done in several steps.

  1. Create a parent directory, likely sharing a name with your project or product, which will contain everything.

  2. Within that directory, run flutter create client. You may also name this Flutter project app, mobile, <project-name>-app or anything else that seems appropriate. At this point, your folder structure should look like this:

    my_project/
      client/
        lib/
          main.dart
        pubspec.yaml
        ...
    
  3. From the same location where you ran flutter create, run dart create -t server-shelf server. You may also name this Shelf project, backend, api, <project-name>-server, or anything else that seems appropriate. At this point, your folder structure should look like this:

    my_project/
      client/
        lib/
          main.dart
        pubspec.yaml
        ...
      server/
        bin/
          server.dart
        Dockerfile
        pubspec.yaml
        ...
    
  4. Enter your server directory (cd server), and run dart create -t package shared. You may also name this package common, domain, <project-name>-shared, or anything else that seems appropriate. At this point, your folder structure should resemble the sample:

    my_project/
      client/
        lib/
          main.dart
        pubspec.yaml
        ...
      server/
        bin/
          server.dart
        shared/
          lib/
            src/
              ...
            shared.dart
          pubspec.yaml
          ...
        Dockerfile
        pubspec.yaml
        ...
    
  5. Next, begin granting access to your shared code by making the following edits to your Flutter app's pubspec.yaml file. Open that file (client/pubspec.yaml) and add the following dependency under the dependencies block:

    dependencies:
      # Add these two lines:
      shared:
        path: ../server/shared
    
  6. Next, finish granting access to your shared code by making the following edits to your server's pubspec.yaml file. Open that file (server/pubspec.yaml) and add the following dependency under the dependencies block:

    dependencies:
      # Add these two lines:
      shared:
        path: ./shared
    
  7. The final step is to adjust your Dockerfile, as it can no longer successfully run dart pub get after only copying over the pubspec.yaml file (that command now requires the entirety of your shared directory to be present.

  • Find the line that says COPY pubspec.* ./, and change it to COPY . ..

With that, you're ready to build and run the app.

Running the sample

To run the sample, or an equivalent you've reconstructed yourself, choose a runtime method below based on your needs.

From the CLI

In one terminal window, run the following commands:

cd my_project/server
dart run bin/server.dart

In a separate terminal window, run the following commands:

cd my_project/client
flutter run

Note: If you named your mobile client and backend servers something other than client and server, you will need to substitute in those values above.

Build and run with Docker

To build your server's Docker image, run the following commands in a terminal window:

cd my_project/server
docker build . -t my_project_server

To run that image as a Docker container, run the following commands in a terminal window:

docker run -it my_project_server

Build and run with docker-compose

If you have docker-compose installed on your machine, you can run the following command to build and launch your server:

cd my_project
docker-compose up -d

And then later stop the server with:

docker-compose stop