Skip to content

Commit 9f1fe74

Browse files
committed
improved the code and null safety
1 parent 471fa86 commit 9f1fe74

File tree

5 files changed

+96
-109
lines changed

5 files changed

+96
-109
lines changed

lib/google_places_flutter.dart

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ import 'package:rxdart/rxdart.dart';
1313

1414
class GooglePlaceAutoCompleteTextField extends StatefulWidget {
1515
InputDecoration inputDecoration;
16-
ItemClick itmClick;
17-
GetPlaceDetailswWithLatLng getPlaceDetailWithLatLng;
16+
ItemClick? itmClick;
17+
GetPlaceDetailswWithLatLng? getPlaceDetailWithLatLng;
1818
bool isLatLngRequired = true;
1919

2020
TextStyle textStyle;
2121
String googleAPIKey;
2222
int debounceTime = 600;
23-
List<String> countries = List();
23+
List<String>? countries = [];
2424
TextEditingController textEditingController = TextEditingController();
2525

2626
GooglePlaceAutoCompleteTextField(
27-
{@required this.textEditingController,
28-
@required this.googleAPIKey,
27+
{required this.textEditingController,
28+
required this.googleAPIKey,
2929
this.debounceTime: 600,
3030
this.inputDecoration: const InputDecoration(),
3131
this.itmClick,
@@ -43,8 +43,8 @@ class GooglePlaceAutoCompleteTextField extends StatefulWidget {
4343
class _GooglePlaceAutoCompleteTextFieldState
4444
extends State<GooglePlaceAutoCompleteTextField> {
4545
final subject = new PublishSubject<String>();
46-
OverlayEntry _overlayEntry;
47-
List<Prediction> alPredictions = new List();
46+
OverlayEntry? _overlayEntry;
47+
List<Prediction> alPredictions = [];
4848

4949
TextEditingController controller = TextEditingController();
5050
final LayerLink _layerLink = LayerLink();
@@ -71,8 +71,8 @@ class _GooglePlaceAutoCompleteTextFieldState
7171
if (widget.countries != null) {
7272
// in
7373

74-
for (int i = 0; i < widget.countries.length; i++) {
75-
String country = widget.countries[i];
74+
for (int i = 0; i < widget.countries!.length; i++) {
75+
String country = widget.countries![i];
7676

7777
if (i == 0) {
7878
url = url + "&components=country:$country";
@@ -90,21 +90,21 @@ class _GooglePlaceAutoCompleteTextFieldState
9090

9191
if (text.length == 0) {
9292
alPredictions.clear();
93-
this._overlayEntry.remove();
93+
this._overlayEntry!.remove();
9494
return;
9595
}
9696

9797
isSearched = false;
98-
if (subscriptionResponse.predictions.length > 0) {
98+
if (subscriptionResponse.predictions!.length > 0) {
9999
alPredictions.clear();
100-
alPredictions.addAll(subscriptionResponse.predictions);
100+
alPredictions.addAll(subscriptionResponse.predictions!);
101101
}
102102

103103
//if (this._overlayEntry == null)
104104

105105
this._overlayEntry = null;
106106
this._overlayEntry = this._createOverlayEntry();
107-
Overlay.of(context).insert(this._overlayEntry);
107+
Overlay.of(context)!.insert(this._overlayEntry!);
108108
// this._overlayEntry.markNeedsBuild();
109109
}
110110

@@ -120,9 +120,9 @@ class _GooglePlaceAutoCompleteTextFieldState
120120
getLocation(text);
121121
}
122122

123-
OverlayEntry _createOverlayEntry() {
123+
OverlayEntry? _createOverlayEntry() {
124124
if (context != null && context.findRenderObject() != null) {
125-
RenderBox renderBox = context.findRenderObject();
125+
RenderBox renderBox = context.findRenderObject() as RenderBox;
126126
var size = renderBox.size;
127127
var offset = renderBox.localToGlobal(Offset.zero);
128128
return OverlayEntry(
@@ -144,7 +144,7 @@ class _GooglePlaceAutoCompleteTextFieldState
144144
return InkWell(
145145
onTap: () {
146146
if (index < alPredictions.length) {
147-
widget.itmClick(alPredictions[index]);
147+
widget.itmClick!(alPredictions[index]);
148148
if (!widget.isLatLngRequired) return;
149149

150150
getPlaceDetailsFromPlaceId(
@@ -155,7 +155,7 @@ class _GooglePlaceAutoCompleteTextFieldState
155155
},
156156
child: Container(
157157
padding: EdgeInsets.all(10),
158-
child: Text(alPredictions[index].description)),
158+
child: Text(alPredictions[index].description!)),
159159
);
160160
},
161161
)),
@@ -168,12 +168,12 @@ class _GooglePlaceAutoCompleteTextFieldState
168168
alPredictions.clear();
169169
this._overlayEntry = this._createOverlayEntry();
170170
if (context != null) {
171-
Overlay.of(context).insert(this._overlayEntry);
172-
this._overlayEntry.markNeedsBuild();
171+
Overlay.of(context)!.insert(this._overlayEntry!);
172+
this._overlayEntry!.markNeedsBuild();
173173
}
174174
}
175175

176-
Future<Response> getPlaceDetailsFromPlaceId(Prediction prediction) async {
176+
Future<Response?> getPlaceDetailsFromPlaceId(Prediction prediction) async {
177177
//String key = GlobalConfiguration().getString('google_maps_key');
178178

179179
var url =
@@ -184,10 +184,10 @@ class _GooglePlaceAutoCompleteTextFieldState
184184

185185
PlaceDetails placeDetails = PlaceDetails.fromJson(response.data);
186186

187-
prediction.lat = placeDetails.result.geometry.location.lat.toString();
188-
prediction.lng = placeDetails.result.geometry.location.lng.toString();
187+
prediction.lat = placeDetails.result!.geometry!.location!.lat.toString();
188+
prediction.lng = placeDetails.result!.geometry!.location!.lng.toString();
189189

190-
widget.getPlaceDetailWithLatLng(prediction);
190+
widget.getPlaceDetailWithLatLng!(prediction);
191191

192192
// prediction.latLng = new LatLng(
193193
// placeDetails.result.geometry.location.lat,
@@ -196,11 +196,11 @@ class _GooglePlaceAutoCompleteTextFieldState
196196
}
197197

198198
PlacesAutocompleteResponse parseResponse(Map responseBody) {
199-
return PlacesAutocompleteResponse.fromJson(responseBody);
199+
return PlacesAutocompleteResponse.fromJson(responseBody as Map<String, dynamic>);
200200
}
201201

202202
PlaceDetails parsePlaceDetailMap(Map responseBody) {
203-
return PlaceDetails.fromJson(responseBody);
203+
return PlaceDetails.fromJson(responseBody as Map<String, dynamic>);
204204
}
205205

206206
typedef ItemClick = void Function(Prediction postalCodeResponse);

lib/model/place_details.dart

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class PlaceDetails {
2-
Result result;
3-
String status;
2+
Result? result;
3+
String? status;
44

55
PlaceDetails({this.result, this.status});
66

@@ -14,29 +14,29 @@ class PlaceDetails {
1414
final Map<String, dynamic> data = new Map<String, dynamic>();
1515

1616
if (this.result != null) {
17-
data['result'] = this.result.toJson();
17+
data['result'] = this.result!.toJson();
1818
}
1919
data['status'] = this.status;
2020
return data;
2121
}
2222
}
2323

2424
class Result {
25-
List<AddressComponents> addressComponents;
26-
String adrAddress;
27-
String formattedAddress;
28-
Geometry geometry;
29-
String icon;
30-
String name;
31-
List<Photos> photos;
32-
String placeId;
33-
String reference;
34-
String scope;
35-
List<String> types;
36-
String url;
37-
int utcOffset;
38-
String vicinity;
39-
String website;
25+
List<AddressComponents>? addressComponents;
26+
String? adrAddress;
27+
String? formattedAddress;
28+
Geometry? geometry;
29+
String? icon;
30+
String? name;
31+
List<Photos>? photos;
32+
String? placeId;
33+
String? reference;
34+
String? scope;
35+
List<String>? types;
36+
String? url;
37+
int? utcOffset;
38+
String? vicinity;
39+
String? website;
4040

4141
Result(
4242
{this.addressComponents,
@@ -57,9 +57,9 @@ class Result {
5757

5858
Result.fromJson(Map<String, dynamic> json) {
5959
if (json['address_components'] != null) {
60-
addressComponents = new List<AddressComponents>();
60+
addressComponents = [];
6161
json['address_components'].forEach((v) {
62-
addressComponents.add(new AddressComponents.fromJson(v));
62+
addressComponents!.add(new AddressComponents.fromJson(v));
6363
});
6464
}
6565
adrAddress = json['adr_address'];
@@ -70,9 +70,9 @@ class Result {
7070
icon = json['icon'];
7171
name = json['name'];
7272
if (json['photos'] != null) {
73-
photos = new List<Photos>();
73+
photos =[];
7474
json['photos'].forEach((v) {
75-
photos.add(new Photos.fromJson(v));
75+
photos!.add(new Photos.fromJson(v));
7676
});
7777
}
7878
placeId = json['place_id'];
@@ -89,17 +89,17 @@ class Result {
8989
final Map<String, dynamic> data = new Map<String, dynamic>();
9090
if (this.addressComponents != null) {
9191
data['address_components'] =
92-
this.addressComponents.map((v) => v.toJson()).toList();
92+
this.addressComponents!.map((v) => v.toJson()).toList();
9393
}
9494
data['adr_address'] = this.adrAddress;
9595
data['formatted_address'] = this.formattedAddress;
9696
if (this.geometry != null) {
97-
data['geometry'] = this.geometry.toJson();
97+
data['geometry'] = this.geometry!.toJson();
9898
}
9999
data['icon'] = this.icon;
100100
data['name'] = this.name;
101101
if (this.photos != null) {
102-
data['photos'] = this.photos.map((v) => v.toJson()).toList();
102+
data['photos'] = this.photos!.map((v) => v.toJson()).toList();
103103
}
104104
data['place_id'] = this.placeId;
105105
data['reference'] = this.reference;
@@ -114,9 +114,9 @@ class Result {
114114
}
115115

116116
class AddressComponents {
117-
String longName;
118-
String shortName;
119-
List<String> types;
117+
String? longName;
118+
String? shortName;
119+
List<String>? types;
120120

121121
AddressComponents({this.longName, this.shortName, this.types});
122122

@@ -136,8 +136,8 @@ class AddressComponents {
136136
}
137137

138138
class Geometry {
139-
Location location;
140-
Viewport viewport;
139+
Location? location;
140+
Viewport? viewport;
141141

142142
Geometry({this.location, this.viewport});
143143

@@ -153,18 +153,18 @@ class Geometry {
153153
Map<String, dynamic> toJson() {
154154
final Map<String, dynamic> data = new Map<String, dynamic>();
155155
if (this.location != null) {
156-
data['location'] = this.location.toJson();
156+
data['location'] = this.location!.toJson();
157157
}
158158
if (this.viewport != null) {
159-
data['viewport'] = this.viewport.toJson();
159+
data['viewport'] = this.viewport!.toJson();
160160
}
161161
return data;
162162
}
163163
}
164164

165165
class Location {
166-
double lat;
167-
double lng;
166+
double? lat;
167+
double? lng;
168168

169169
Location({this.lat, this.lng});
170170

@@ -182,8 +182,8 @@ class Location {
182182
}
183183

184184
class Viewport {
185-
Location northeast;
186-
Location southwest;
185+
Location? northeast;
186+
Location? southwest;
187187

188188
Viewport({this.northeast, this.southwest});
189189

@@ -199,20 +199,20 @@ class Viewport {
199199
Map<String, dynamic> toJson() {
200200
final Map<String, dynamic> data = new Map<String, dynamic>();
201201
if (this.northeast != null) {
202-
data['northeast'] = this.northeast.toJson();
202+
data['northeast'] = this.northeast!.toJson();
203203
}
204204
if (this.southwest != null) {
205-
data['southwest'] = this.southwest.toJson();
205+
data['southwest'] = this.southwest!.toJson();
206206
}
207207
return data;
208208
}
209209
}
210210

211211
class Photos {
212-
int height;
213-
List<String> htmlAttributions;
214-
String photoReference;
215-
int width;
212+
int? height;
213+
List<String>? htmlAttributions;
214+
String? photoReference;
215+
int? width;
216216

217217
Photos({this.height, this.htmlAttributions, this.photoReference, this.width});
218218

0 commit comments

Comments
 (0)