Skip to content

Commit 88c722c

Browse files
dkbastvlidholt
andauthored
feat: Improve new Serverpod project template (serverpod#3408)
Co-authored-by: Viktor Lidholt <[email protected]>
1 parent 7998e09 commit 88c722c

38 files changed

+280
-862
lines changed

.fvmrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"flutter": "3.19.0"
3+
}

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,7 @@ templates/serverpod_templates/projectname_flutter/android
3838
templates/serverpod_templates/projectname_flutter/ios
3939
templates/serverpod_templates/projectname_flutter/macos
4040
templates/serverpod_templates/projectname_server/deploy/gcp/.terraform
41+
templates/serverpod_templates/projectname_client/lib/src/
42+
templates/serverpod_templates/projectname_server/lib/src/generated/
43+
templates/serverpod_templates/projectname_server/test/integration/test_tools/
44+
templates/serverpod_templates/projectname_server_upgrade/test/

templates/serverpod_templates/projectname_client/analysis_options.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
# - camel_case_types
1010

1111
analyzer:
12-
# exclude:
13-
# - path/to/excluded/files/**
12+
exclude:
13+
- lib/src/protocol/**

templates/serverpod_templates/projectname_client/doc/endpoint.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ Each class contains callable methods that will call a method on the server side.
55
Example usage:
66

77
```dart
8-
// How to use ExampleEndpoint.
9-
client.example.hello("world!");
8+
// How to use GreetingEndpoint.
9+
client.greeting.hello("world!");
1010
1111
// Generic format.
1212
client.<endpoint>.<method>(...);

templates/serverpod_templates/projectname_client/lib/src/protocol/client.dart

-66
This file was deleted.

templates/serverpod_templates/projectname_client/lib/src/protocol/example.dart

-79
This file was deleted.

templates/serverpod_templates/projectname_client/lib/src/protocol/protocol.dart

-60
This file was deleted.

templates/serverpod_templates/projectname_flutter/analysis_options.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,5 @@ linter:
2424
rules:
2525
# avoid_print: false # Uncomment to disable the `avoid_print` rule
2626
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule
27-
2827
# Additional information about this file can be found at
2928
# https://dart.dev/guides/language/analysis-options

templates/serverpod_templates/projectname_flutter/lib/main.dart

+44-24
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,30 @@ import 'package:projectname_client/projectname_client.dart';
22
import 'package:flutter/material.dart';
33
import 'package:serverpod_flutter/serverpod_flutter.dart';
44

5-
// Sets up a singleton client object that can be used to talk to the server from
6-
// anywhere in our app. The client is generated from your server code.
7-
// The client is set up to connect to a Serverpod running on a local server on
8-
// the default port. You will need to modify this to connect to staging or
9-
// production servers.
10-
var client = Client('http://$localhost:8080/')
11-
..connectivityMonitor = FlutterConnectivityMonitor();
5+
/// Sets up a global client object that can be used to talk to the server from
6+
/// anywhere in our app. The client is generated from your server code
7+
/// and is set up to connect to a Serverpod running on a local server on
8+
/// the default port. You will need to modify this to connect to staging or
9+
/// production servers.
10+
/// In a larger app, you may want to use the dependency injection of your choice instead of
11+
/// using a global client object. This is just a simple example.
12+
late final Client client;
13+
14+
late String serverUrl;
1215

1316
void main() {
17+
// When you are running the app on a physical device, you need to set the
18+
// server URL to the IP address of your computer. You can find the IP
19+
// address by running `ipconfig` on Windows or `ifconfig` on Mac/Linux.
20+
// You can set the variable when running or building your app like this:
21+
// E.g. `flutter run --dart-define=SERVER_URL=https://api.example.com/`
22+
const serverUrlFromEnv = String.fromEnvironment('SERVER_URL');
23+
final serverUrl =
24+
serverUrlFromEnv.isEmpty ? 'http://$localhost:8080/' : serverUrlFromEnv;
25+
26+
client = Client(serverUrl)
27+
..connectivityMonitor = FlutterConnectivityMonitor();
28+
1429
runApp(const MyApp());
1530
}
1631

@@ -39,22 +54,24 @@ class MyHomePage extends StatefulWidget {
3954
}
4055

4156
class MyHomePageState extends State<MyHomePage> {
42-
// These fields hold the last result or error message that we've received from
43-
// the server or null if no result exists yet.
57+
/// Holds the last result or null if no result exists yet.
4458
String? _resultMessage;
59+
60+
/// Holds the last error message that we've received from the server or null if no
61+
/// error exists yet.
4562
String? _errorMessage;
4663

4764
final _textEditingController = TextEditingController();
4865

49-
// Calls the `hello` method of the `example` endpoint. Will set either the
50-
// `_resultMessage` or `_errorMessage` field, depending on if the call
51-
// is successful.
66+
/// Calls the `hello` method of the `greeting` endpoint. Will set either the
67+
/// `_resultMessage` or `_errorMessage` field, depending on if the call
68+
/// is successful.
5269
void _callHello() async {
5370
try {
54-
final result = await client.example.hello(_textEditingController.text);
71+
final result = await client.greeting.hello(_textEditingController.text);
5572
setState(() {
5673
_errorMessage = null;
57-
_resultMessage = result;
74+
_resultMessage = result.message;
5875
});
5976
} catch (e) {
6077
setState(() {
@@ -89,7 +106,7 @@ class MyHomePageState extends State<MyHomePage> {
89106
child: const Text('Send to Server'),
90107
),
91108
),
92-
_ResultDisplay(
109+
ResultDisplay(
93110
resultMessage: _resultMessage,
94111
errorMessage: _errorMessage,
95112
),
@@ -100,13 +117,14 @@ class MyHomePageState extends State<MyHomePage> {
100117
}
101118
}
102119

103-
// _ResultDisplays shows the result of the call. Either the returned result from
104-
// the `example.hello` endpoint method or an error message.
105-
class _ResultDisplay extends StatelessWidget {
120+
/// ResultDisplays shows the result of the call. Either the returned result from
121+
/// the `example.greeting` endpoint method or an error message.
122+
class ResultDisplay extends StatelessWidget {
106123
final String? resultMessage;
107124
final String? errorMessage;
108125

109-
const _ResultDisplay({
126+
const ResultDisplay({
127+
super.key,
110128
this.resultMessage,
111129
this.errorMessage,
112130
});
@@ -126,11 +144,13 @@ class _ResultDisplay extends StatelessWidget {
126144
text = 'No server response yet.';
127145
}
128146

129-
return Container(
130-
height: 50,
131-
color: backgroundColor,
132-
child: Center(
133-
child: Text(text),
147+
return ConstrainedBox(
148+
constraints: const BoxConstraints(minHeight: 50),
149+
child: Container(
150+
color: backgroundColor,
151+
child: Center(
152+
child: Text(text),
153+
),
134154
),
135155
);
136156
}

templates/serverpod_templates/projectname_server/analysis_options.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ include: package:lints/recommended.yaml
1919
# rules:
2020
# - camel_case_types
2121

22-
# analyzer:
23-
# exclude:
24-
# - path/to/excluded/files/**
25-
22+
analyzer:
23+
exclude:
24+
- lib/src/generated/**
25+
- test/integration/test_tools/serverpod_test_tools.dart
2626
# For more information about the core and recommended set of lints, see
2727
# https://dart.dev/go/core-lints
2828

templates/serverpod_templates/projectname_server/lib/server.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import 'package:serverpod/serverpod.dart';
33
import 'src/generated/protocol.dart';
44
import 'src/generated/endpoints.dart';
55

6-
// This is the starting point of your Serverpod server.
6+
/// The starting point of the Serverpod server.
77
void run(List<String> args) async {
88
// Initialize Serverpod and connect it with your generated code.
99
final pod = Serverpod(

0 commit comments

Comments
 (0)