@@ -6,32 +6,30 @@ import 'dart:html';
6
6
import 'package:react/react.dart' as react;
7
7
import 'package:react/react_dom.dart' as react_dom;
8
8
9
- /**
10
- * Hello,
11
- *
12
- * This is the Dart portion of the tutorial for the Dart react package.
13
- *
14
- * We'll go through a simple app that queries the Google Maps API and shows the result to the user.
15
- * It also stores the search history and allows the user to reload past queries.
16
- *
17
- * In this file you'll find the structure and the logic of the app. There is also a `geocodes.html` file which contains
18
- * the mount point.
19
- *
20
- * Be sure that you understand the basic concepts of [React] (http://facebook.github.io/react/) before reading this
21
- * tutorial.
22
- *
23
- * Enjoy!
24
- */
9
+ /// Hello,
10
+ ///
11
+ /// This is the Dart portion of the tutorial for the Dart react package.
12
+ ///
13
+ /// We'll go through a simple app that queries the Google Maps API and shows the result to the user.
14
+ /// It also stores the search history and allows the user to reload past queries.
15
+ ///
16
+ /// In this file you'll find the structure and the logic of the app. There is also a `geocodes.html` file which contains
17
+ /// the mount point.
18
+ ///
19
+ /// Be sure that you understand the basic concepts of [React] (https://reactjs.org/) before reading this
20
+ /// tutorial.
21
+ ///
22
+ /// Enjoy!
25
23
26
24
/// Divide your app into components and conquer!
27
25
///
28
- /// This is the first custom [ Component] .
26
+ /// This is the first custom ` Component` .
29
27
///
30
28
/// It is just an HTML `<tr>` element displaying a single API response to the user.
31
29
class _GeocodesResultItem extends react.Component {
32
30
/// The only function you must implement in the custom component is [render] .
33
31
///
34
- /// Every [ Component] has a map of properties called [Component. props] . It can be specified during creation.
32
+ /// Every ` Component` has a map of properties called ` props` . It can be specified during creation.
35
33
@override
36
34
render () {
37
35
return react.tr ({}, [
@@ -42,13 +40,13 @@ class _GeocodesResultItem extends react.Component {
42
40
}
43
41
}
44
42
45
- /// Now we need to tell ReactJS that our custom [ Component] exists by calling [ registerComponent] .
43
+ /// Now we need to tell ReactJS that our custom ` Component` exists by calling ` registerComponent` .
46
44
///
47
45
/// As a reward, it gives us a function, that takes the properties and returns our element. You'll see it in action
48
46
/// shortly.
49
47
///
50
- /// This is the only correct way to create a [ Component] . Do not use the constructor!
51
- var geocodesResultItem = react.registerComponent (() => new _GeocodesResultItem ());
48
+ /// This is the only correct way to create a ` Component` . Do not use the constructor!
49
+ var geocodesResultItem = react.registerComponent (() => _GeocodesResultItem ());
52
50
53
51
/// In this component we'll build an HTML `<table>` element full of the `<tr>` elements generated by
54
52
/// [_GeocodesResultItem]
@@ -95,13 +93,13 @@ class _GeocodesResultList extends react.Component {
95
93
}
96
94
}
97
95
98
- var geocodesResultList = react.registerComponent (() => new _GeocodesResultList ());
96
+ var geocodesResultList = react.registerComponent (() => _GeocodesResultList ());
99
97
100
- /// In this [ Component] we'll build a search form.
98
+ /// In this ` Component` we'll build a search form.
101
99
///
102
- /// This [ Component] illustrates that:
100
+ /// This ` Component` illustrates that:
103
101
///
104
- /// > The functions can be [ Component] parameters _(handy for callbacks)_
102
+ /// > The functions can be ` Component` parameters _(handy for callbacks)_
105
103
///
106
104
/// > The DOM [Element] s can be accessed using `ref` s.
107
105
class _GeocodesForm extends react.Component {
@@ -158,16 +156,16 @@ class _GeocodesForm extends react.Component {
158
156
/// Handle form submission via `props.onSubmit`
159
157
onFormSubmit (react.SyntheticEvent event) {
160
158
event.preventDefault ();
161
- InputElement inputElement = react_dom.findDOMNode (searchInputInstance);
159
+ final inputElement = react_dom.findDOMNode (searchInputInstance);
162
160
// The input's value is accessed.
163
- var address = inputElement.value;
161
+ final address = inputElement.value;
164
162
inputElement.value = '' ;
165
163
// Call the callback from the parent element is called.
166
164
props['submitter' ](address);
167
165
}
168
166
}
169
167
170
- var geocodesForm = react.registerComponent (() => new _GeocodesForm ());
168
+ var geocodesForm = react.registerComponent (() => _GeocodesForm ());
171
169
172
170
/// Renders an HTML `<li>` to be used as a child within the [_GeocodesHistoryList] .
173
171
class _GeocodesHistoryItem extends react.Component {
@@ -189,7 +187,7 @@ class _GeocodesHistoryItem extends react.Component {
189
187
}
190
188
}
191
189
192
- var geocodesHistoryItem = react.registerComponent (() => new _GeocodesHistoryItem ());
190
+ var geocodesHistoryItem = react.registerComponent (() => _GeocodesHistoryItem ());
193
191
194
192
/// Renders the "history list"
195
193
///
@@ -203,7 +201,7 @@ class _GeocodesHistoryList extends react.Component {
203
201
{
204
202
'key' : 'list' ,
205
203
},
206
- new List .from (props['data' ].keys.map ((key) => geocodesHistoryItem ({
204
+ List .from (( props['data' ] as Map ) .keys.map ((key) => geocodesHistoryItem ({
207
205
'key' : key,
208
206
'query' : props['data' ][key]['query' ],
209
207
'status' : props['data' ][key]['status' ],
@@ -214,11 +212,11 @@ class _GeocodesHistoryList extends react.Component {
214
212
}
215
213
}
216
214
217
- var geocodesHistoryList = react.registerComponent (() => new _GeocodesHistoryList ());
215
+ var geocodesHistoryList = react.registerComponent (() => _GeocodesHistoryList ());
218
216
219
- /// The root [ Component] of our application.
217
+ /// The root ` Component` of our application.
220
218
///
221
- /// Introduces [state] . Both [state] and [props] are valid locations to store [ Component] data.
219
+ /// Introduces [state] . Both [state] and [props] are valid locations to store ` Component` data.
222
220
///
223
221
/// However, there are some key differences to note:
224
222
///
@@ -228,7 +226,7 @@ var geocodesHistoryList = react.registerComponent(() => new _GeocodesHistoryList
228
226
///
229
227
/// > When [state] is changed, the component will re-render.
230
228
///
231
- /// It's a common practice to store the application data in the [state] of the root [ Component] . It will re-render
229
+ /// It's a common practice to store the application data in the [state] of the root ` Component` . It will re-render
232
230
/// every time the state is changed. However, it is not required - you can also use normal variables and re-render
233
231
/// manually if you wish.
234
232
///
@@ -247,23 +245,23 @@ class _GeocodesApp extends react.Component {
247
245
/// Sends the [addressQuery] to the API and processes the result
248
246
newQuery (String addressQuery) async {
249
247
// Once the query is being sent, it appears in the history and is given an id.
250
- var id = addQueryToHistory (addressQuery);
248
+ final id = addQueryToHistory (addressQuery);
251
249
252
250
// Prepare the URL
253
251
addressQuery = Uri .encodeQueryComponent (addressQuery);
254
- var path = 'https://maps.googleapis.com/maps/api/geocode/json?address=$addressQuery ' ;
252
+ final path = 'https://maps.googleapis.com/maps/api/geocode/json?address=$addressQuery ' ;
255
253
256
254
try {
257
255
// Send the request
258
- var raw = await HttpRequest .getString (path);
256
+ final raw = await HttpRequest .getString (path);
259
257
// Delay the answer 2 more seconds, for test purposes
260
- await new Future .delayed (new Duration (seconds: 2 ));
258
+ await Future .delayed (Duration (seconds: 2 ));
261
259
// Is this the answer to the last request?
262
260
if (id == last_id) {
263
261
// If yes, query was `OK` and `shown_addresses` are replaced
264
262
state['history' ][id]['status' ] = 'OK' ;
265
263
266
- var data = json.decode (raw);
264
+ final data = json.decode (raw);
267
265
268
266
// Calling `setState` will update the state and then repaint the component.
269
267
//
@@ -291,7 +289,7 @@ class _GeocodesApp extends react.Component {
291
289
292
290
/// Add a new [addressQuery] to the `state.history` Map with its status set to 'pending', then return its `id` .
293
291
addQueryToHistory (String addressQuery) {
294
- var id = ++ last_id;
292
+ final id = ++ last_id;
295
293
296
294
state['history' ][id] = {'query' : addressQuery, 'status' : 'pending' };
297
295
@@ -324,7 +322,7 @@ class _GeocodesApp extends react.Component {
324
322
}
325
323
}
326
324
327
- var geocodesApp = react.registerComponent (() => new _GeocodesApp ());
325
+ var geocodesApp = react.registerComponent (() => _GeocodesApp ());
328
326
329
327
/// And finally, a few magic commands to wire it all up!
330
328
///
0 commit comments