@@ -2,15 +2,30 @@ import 'package:projectname_client/projectname_client.dart';
2
2
import 'package:flutter/material.dart' ;
3
3
import 'package:serverpod_flutter/serverpod_flutter.dart' ;
4
4
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;
12
15
13
16
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
+
14
29
runApp (const MyApp ());
15
30
}
16
31
@@ -39,22 +54,24 @@ class MyHomePage extends StatefulWidget {
39
54
}
40
55
41
56
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.
44
58
String ? _resultMessage;
59
+
60
+ /// Holds the last error message that we've received from the server or null if no
61
+ /// error exists yet.
45
62
String ? _errorMessage;
46
63
47
64
final _textEditingController = TextEditingController ();
48
65
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.
52
69
void _callHello () async {
53
70
try {
54
- final result = await client.example .hello (_textEditingController.text);
71
+ final result = await client.greeting .hello (_textEditingController.text);
55
72
setState (() {
56
73
_errorMessage = null ;
57
- _resultMessage = result;
74
+ _resultMessage = result.message ;
58
75
});
59
76
} catch (e) {
60
77
setState (() {
@@ -89,7 +106,7 @@ class MyHomePageState extends State<MyHomePage> {
89
106
child: const Text ('Send to Server' ),
90
107
),
91
108
),
92
- _ResultDisplay (
109
+ ResultDisplay (
93
110
resultMessage: _resultMessage,
94
111
errorMessage: _errorMessage,
95
112
),
@@ -100,13 +117,14 @@ class MyHomePageState extends State<MyHomePage> {
100
117
}
101
118
}
102
119
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 {
106
123
final String ? resultMessage;
107
124
final String ? errorMessage;
108
125
109
- const _ResultDisplay ({
126
+ const ResultDisplay ({
127
+ super .key,
110
128
this .resultMessage,
111
129
this .errorMessage,
112
130
});
@@ -126,11 +144,13 @@ class _ResultDisplay extends StatelessWidget {
126
144
text = 'No server response yet.' ;
127
145
}
128
146
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
+ ),
134
154
),
135
155
);
136
156
}
0 commit comments