Skip to content

Commit 64cff60

Browse files
add flutter example app (#58)
1 parent 4798b71 commit 64cff60

20 files changed

+983
-3
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,10 @@ When utilizing the `nitric new` command to initiate a new project, the available
7878

7979
### Dart
8080

81-
| Name | Description | Features |
82-
| ---------------------------------- | ---------------- | -------- |
83-
| [dart-starter](./v1/dart-starter/) | REST API Starter | APIs |
81+
| Name | Description | Features |
82+
| ---------------------------------- | ----------------------------------------------- | ---------------------- |
83+
| [dart-starter](./v1/dart-starter/) | REST API Starter | APIs |
84+
| [flutter](./v1/flutter/) | Basic Flutter Application with a Nitric backend | APIs, Key Value Stores |
8485

8586
### Go
8687

v1/flutter/.gitignore

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Miscellaneous
2+
*.class
3+
*.log
4+
*.pyc
5+
*.swp
6+
.DS_Store
7+
.atom/
8+
.buildlog/
9+
.history
10+
.svn/
11+
migrate_working_dir/
12+
13+
# IntelliJ related
14+
*.iml
15+
*.ipr
16+
*.iws
17+
.idea/
18+
19+
# The .vscode folder contains launch configuration and tasks you configure in
20+
# VS Code which you may wish to be included in version control, so this line
21+
# is commented out by default.
22+
#.vscode/
23+
24+
# Flutter/Dart/Pub related
25+
**/doc/api/
26+
**/ios/Flutter/.last_build_id
27+
.dart_tool/
28+
.flutter-plugins
29+
.flutter-plugins-dependencies
30+
.pub-cache/
31+
.pub/
32+
/build/
33+
34+
# Symbolication related
35+
app.*.symbols
36+
37+
# Obfuscation related
38+
app.*.map.json
39+
40+
# Android Studio will place build artifacts here
41+
/android/app/debug
42+
/android/app/profile
43+
/android/app/release

v1/flutter/.metadata

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# This file tracks properties of this Flutter project.
2+
# Used by Flutter tool to assess capabilities and perform upgrades etc.
3+
#
4+
# This file should be version controlled and should not be manually edited.
5+
6+
version:
7+
revision: "b0850beeb25f6d5b10426284f506557f66181b36"
8+
channel: "stable"
9+
10+
project_type: app
11+
12+
# Tracks metadata for the flutter migrate command
13+
migration:
14+
platforms:
15+
- platform: root
16+
create_revision: b0850beeb25f6d5b10426284f506557f66181b36
17+
base_revision: b0850beeb25f6d5b10426284f506557f66181b36
18+
- platform: android
19+
create_revision: b0850beeb25f6d5b10426284f506557f66181b36
20+
base_revision: b0850beeb25f6d5b10426284f506557f66181b36
21+
- platform: ios
22+
create_revision: b0850beeb25f6d5b10426284f506557f66181b36
23+
base_revision: b0850beeb25f6d5b10426284f506557f66181b36
24+
- platform: linux
25+
create_revision: b0850beeb25f6d5b10426284f506557f66181b36
26+
base_revision: b0850beeb25f6d5b10426284f506557f66181b36
27+
- platform: macos
28+
create_revision: b0850beeb25f6d5b10426284f506557f66181b36
29+
base_revision: b0850beeb25f6d5b10426284f506557f66181b36
30+
- platform: web
31+
create_revision: b0850beeb25f6d5b10426284f506557f66181b36
32+
base_revision: b0850beeb25f6d5b10426284f506557f66181b36
33+
- platform: windows
34+
create_revision: b0850beeb25f6d5b10426284f506557f66181b36
35+
base_revision: b0850beeb25f6d5b10426284f506557f66181b36
36+
37+
# User provided section
38+
39+
# List of Local paths (relative to this file) that should be
40+
# ignored by the migrate tool.
41+
#
42+
# Files that are not part of the templates will be ignored by default.
43+
unmanaged_files:
44+
- 'lib/main.dart'
45+
- 'ios/Runner.xcodeproj/project.pbxproj'

v1/flutter/README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<p align="center">
2+
<a href="https://nitric.io">
3+
<img src="https://raw.githubusercontent.com/nitrictech/nitric/main/docs/assets/nitric-logo.svg" width="120" alt="Nitric Logo"/>
4+
</a>
5+
</p>
6+
7+
<p align="center">
8+
A fast & fun way to build portable cloud-native applications
9+
</p>
10+
11+
<p align="center">
12+
<img alt="GitHub release (latest SemVer)" src="https://img.shields.io/github/v/release/nitrictech/nitric?sort=semver">
13+
<a href="https://twitter.com/nitric_io">
14+
<img alt="Twitter Follow" src="https://img.shields.io/twitter/follow/nitric_io?label=Follow&style=social">
15+
</a>
16+
<a href="https://nitric.io/chat"><img alt="Discord" src="https://img.shields.io/discord/955259353043173427?label=discord"></a>
17+
</p>
18+
19+
## Project Description
20+
21+
A basic Flutter application that uses Nitric as the backend service for the API and key value collections.
22+
23+
## Running this project
24+
25+
To run this project you'll need the [Nitric CLI](https://nitric.io/docs/installation) installed, then you can use the CLI commands to run, build or deploy the project.
26+
27+
You'll also want to make sure the project's required dependencies have been installed.
28+
29+
```bash
30+
# install dependencies
31+
dart pub get
32+
33+
# run locally
34+
nitric start
35+
```
36+
37+
## About Nitric
38+
39+
[Nitric](https://nitric.io) is a framework for rapid development of cloud-native and serverless applications. Define your apps in terms of the resources they need, then write the code for serverless function based APIs, event subscribers and scheduled jobs.
40+
41+
Apps built with Nitric can be deployed to AWS, Azure or Google Cloud all from the same code base so you can focus on your products, not your cloud provider.
42+
43+
Nitric makes it easy to:
44+
45+
- Create smart serverless functions and APIs
46+
- Build reliable distributed apps that use events and/or queues
47+
- Securely store and retrieve secrets
48+
- Read and write files from buckets
49+
50+
## Documentation
51+
52+
The full documentation is available at [nitric.io/docs](https://nitric.io/docs).
53+
54+
We're completely open-source and encourage [code contributions](https://nitric.io/docs/contributions).
55+
56+
## Get in touch
57+
58+
- Jump into our [Discord server](https://nitric.io/chat)
59+
60+
- Ask questions in [GitHub discussions](https://github.com/nitrictech/nitric/discussions)
61+
62+
- Find us on [Twitter](https://twitter.com/nitric_io)
63+
64+
- Send us an [email](mailto:[email protected])

v1/flutter/analysis_options.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# This file configures the analyzer, which statically analyzes Dart code to
2+
# check for errors, warnings, and lints.
3+
#
4+
# The issues identified by the analyzer are surfaced in the UI of Dart-enabled
5+
# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be
6+
# invoked from the command line by running `flutter analyze`.
7+
8+
# The following line activates a set of recommended lints for Flutter apps,
9+
# packages, and plugins designed to encourage good coding practices.
10+
include: package:flutter_lints/flutter.yaml
11+
12+
linter:
13+
# The lint rules applied to this project can be customized in the
14+
# section below to disable rules from the `package:flutter_lints/flutter.yaml`
15+
# included above or to enable additional rules. A list of all available lints
16+
# and their documentation is published at https://dart.dev/lints.
17+
#
18+
# Instead of disabling a lint rule for the entire project in the
19+
# section below, it can also be suppressed for a single line of code
20+
# or a specific dart file by using the `// ignore: name_of_lint` and
21+
# `// ignore_for_file: name_of_lint` syntax on the line or in the file
22+
# producing the lint.
23+
rules:
24+
# avoid_print: false # Uncomment to disable the `avoid_print` rule
25+
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule
26+
27+
# Additional information about this file can be found at
28+
# https://dart.dev/guides/language/analysis-options
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
FROM dart:stable AS build
2+
3+
# The Nitric CLI will provide the HANDLER arg with the location of our service
4+
ARG HANDLER
5+
WORKDIR /app
6+
7+
ENV DEBIAN_FRONTEND=noninteractive
8+
9+
# download Flutter SDK from Flutter Github repo
10+
RUN git clone https://github.com/flutter/flutter.git /usr/local/flutter
11+
12+
ENV DEBIAN_FRONTEND=dialog
13+
14+
# Set flutter environment path
15+
ENV PATH="/usr/local/flutter/bin:/usr/local/flutter/bin/cache/dart-sdk/bin:${PATH}"
16+
17+
# Run flutter doctor
18+
RUN flutter doctor
19+
20+
# Resolve app dependencies.
21+
COPY pubspec.* ./
22+
RUN flutter pub get
23+
24+
# Ensure the ./bin folder exists
25+
RUN mkdir -p ./bin
26+
27+
# Copy app source code and AOT compile it.
28+
COPY . .
29+
# Ensure packages are still up-to-date if anything has changed
30+
RUN flutter pub get --offline
31+
# Compile the dart service into an exe
32+
RUN dart compile exe ./${HANDLER} -o bin/main
33+
34+
# Start from scratch and copy in the necessary runtime files
35+
FROM alpine
36+
37+
COPY --from=build /runtime/ /
38+
COPY --from=build /app/bin/main /app/bin/
39+
40+
ENTRYPOINT ["/app/bin/main"]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
build
2+
test
3+
.nitric
4+
.idea
5+
.dart_tool
6+
.git
7+
docker
8+
android
9+
ios
10+
linux
11+
macos
12+
web
13+
windows

v1/flutter/lib/cors.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import 'package:nitric_sdk/nitric.dart';
2+
3+
/// Handle Preflight Options requests by returning status 200 to the requests
4+
Future<HttpContext> optionsHandler(HttpContext ctx) async {
5+
ctx.res.headers["Content-Type"] = ["text/html; charset=ascii"];
6+
ctx.res.body = "OK";
7+
8+
return ctx.next();
9+
}
10+
11+
/// Add CORS headers to responses
12+
Future<HttpContext> addCors(HttpContext ctx) async {
13+
ctx.res.headers["Access-Control-Allow-Origin"] = ["*"];
14+
ctx.res.headers["Access-Control-Allow-Headers"] = [
15+
"Origin, X-Requested-With, Content-Type, Accept, Authorization",
16+
];
17+
ctx.res.headers["Access-Control-Allow-Methods"] = [
18+
"GET, PUT, POST, PATCH, OPTIONS, DELETE",
19+
];
20+
ctx.res.headers["Access-Control-Max-Age"] = ["7200"];
21+
22+
return ctx.next();
23+
}

v1/flutter/lib/favorite.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Favorite {
2+
/// The name of the favorite
3+
String name;
4+
5+
Favorite(this.name);
6+
7+
/// Convert a json decodable map to a Favorite object
8+
Favorite.fromJson(Map<String, dynamic> json) : name = json['name'];
9+
10+
/// Convert a Favorite object to a json encodable
11+
static Map<String, dynamic> toJson(Favorite favorite) =>
12+
{'name': favorite.name};
13+
}

v1/flutter/lib/main.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:provider/provider.dart';
3+
import 'package:word_generator/pages/home.dart';
4+
import 'package:word_generator/providers/favorites.dart';
5+
import 'package:word_generator/providers/word.dart';
6+
7+
void main() => runApp(Application());
8+
9+
class Application extends StatelessWidget {
10+
const Application({super.key});
11+
@override
12+
Widget build(BuildContext context) {
13+
return MultiProvider(
14+
providers: [
15+
ChangeNotifierProvider(create: (context) => FavoritesProvider()),
16+
ChangeNotifierProvider(create: (context) => WordProvider()),
17+
],
18+
child: MaterialApp(
19+
title: 'Word Generator App',
20+
theme: ThemeData(
21+
useMaterial3: true,
22+
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
23+
),
24+
home: HomePage(), // <-- Change here
25+
),
26+
);
27+
}
28+
}

0 commit comments

Comments
 (0)