Skip to content

Commit

Permalink
updated to latest flutter; example updated with null safety; latest h…
Browse files Browse the repository at this point in the history
…ttp package
  • Loading branch information
tylersavery committed May 30, 2024
1 parent 5971b55 commit 26be92a
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 135 deletions.
57 changes: 24 additions & 33 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:google_place/google_place.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await DotEnv().load('.env');
await DotEnv().load(fileName: '.env');
runApp(MyApp());
}

Expand All @@ -29,12 +29,12 @@ class HomePage extends StatefulWidget {
}

class _HomePageState extends State<HomePage> {
GooglePlace googlePlace;
late final GooglePlace googlePlace;
List<AutocompletePrediction> predictions = [];

@override
void initState() {
String apiKey = DotEnv().env['API_KEY'];
String apiKey = dotenv.env['API_KEY'] ?? '';
googlePlace = GooglePlace(apiKey);
super.initState();
}
Expand Down Expand Up @@ -90,14 +90,14 @@ class _HomePageState extends State<HomePage> {
color: Colors.white,
),
),
title: Text(predictions[index].description),
title: Text(predictions[index].description ?? ""),
onTap: () {
debugPrint(predictions[index].placeId);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailsPage(
placeId: predictions[index].placeId,
placeId: predictions[index].placeId ?? "",
googlePlace: googlePlace,
),
),
Expand All @@ -122,7 +122,7 @@ class _HomePageState extends State<HomePage> {
var result = await googlePlace.autocomplete.get(value);
if (result != null && result.predictions != null && mounted) {
setState(() {
predictions = result.predictions;
predictions = result.predictions ?? [];
});
}
}
Expand All @@ -132,11 +132,10 @@ class DetailsPage extends StatefulWidget {
final String placeId;
final GooglePlace googlePlace;

DetailsPage({Key key, this.placeId, this.googlePlace}) : super(key: key);
DetailsPage({Key? key, required this.placeId, required this.googlePlace}) : super(key: key);

@override
_DetailsPageState createState() =>
_DetailsPageState(this.placeId, this.googlePlace);
_DetailsPageState createState() => _DetailsPageState(this.placeId, this.googlePlace);
}

class _DetailsPageState extends State<DetailsPage> {
Expand All @@ -145,7 +144,7 @@ class _DetailsPageState extends State<DetailsPage> {

_DetailsPageState(this.placeId, this.googlePlace);

DetailsResult detailsResult;
DetailsResult? detailsResult;
List<Uint8List> images = [];

@override
Expand Down Expand Up @@ -220,19 +219,19 @@ class _DetailsPageState extends State<DetailsPage> {
),
),
),
detailsResult != null && detailsResult.types != null
detailsResult != null && detailsResult!.types != null
? Container(
margin: EdgeInsets.only(left: 15, top: 10),
height: 50,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: detailsResult.types.length,
itemCount: detailsResult!.types!.length,
itemBuilder: (context, index) {
return Container(
margin: EdgeInsets.only(right: 10),
child: Chip(
label: Text(
detailsResult.types[index],
detailsResult!.types![index],
style: TextStyle(
color: Colors.white,
),
Expand All @@ -251,9 +250,8 @@ class _DetailsPageState extends State<DetailsPage> {
child: Icon(Icons.location_on),
),
title: Text(
detailsResult != null &&
detailsResult.formattedAddress != null
? 'Address: ${detailsResult.formattedAddress}'
detailsResult != null && detailsResult!.formattedAddress != null
? 'Address: ${detailsResult!.formattedAddress}'
: "Address: null",
),
),
Expand All @@ -265,10 +263,8 @@ class _DetailsPageState extends State<DetailsPage> {
child: Icon(Icons.location_searching),
),
title: Text(
detailsResult != null &&
detailsResult.geometry != null &&
detailsResult.geometry.location != null
? 'Geometry: ${detailsResult.geometry.location.lat.toString()},${detailsResult.geometry.location.lng.toString()}'
detailsResult != null && detailsResult!.geometry != null && detailsResult!.geometry!.location != null
? 'Geometry: ${detailsResult!.geometry!.location!.lat.toString()},${detailsResult!.geometry!.location!.lng.toString()}'
: "Geometry: null",
),
),
Expand All @@ -280,9 +276,8 @@ class _DetailsPageState extends State<DetailsPage> {
child: Icon(Icons.timelapse),
),
title: Text(
detailsResult != null &&
detailsResult.utcOffset != null
? 'UTC offset: ${detailsResult.utcOffset.toString()} min'
detailsResult != null && detailsResult!.utcOffset != null
? 'UTC offset: ${detailsResult!.utcOffset.toString()} min'
: "UTC offset: null",
),
),
Expand All @@ -294,10 +289,7 @@ class _DetailsPageState extends State<DetailsPage> {
child: Icon(Icons.rate_review),
),
title: Text(
detailsResult != null &&
detailsResult.rating != null
? 'Rating: ${detailsResult.rating.toString()}'
: "Rating: null",
detailsResult != null && detailsResult!.rating != null ? 'Rating: ${detailsResult!.rating.toString()}' : "Rating: null",
),
),
),
Expand All @@ -308,9 +300,8 @@ class _DetailsPageState extends State<DetailsPage> {
child: Icon(Icons.attach_money),
),
title: Text(
detailsResult != null &&
detailsResult.priceLevel != null
? 'Price level: ${detailsResult.priceLevel.toString()}'
detailsResult != null && detailsResult!.priceLevel != null
? 'Price level: ${detailsResult!.priceLevel.toString()}'
: "Price level: null",
),
),
Expand Down Expand Up @@ -338,16 +329,16 @@ class _DetailsPageState extends State<DetailsPage> {
images = [];
});

if (result.result.photos != null) {
for (var photo in result.result.photos) {
if (result.result!.photos != null) {
for (var photo in (result.result!.photos ?? [])) {
getPhoto(photo.photoReference);
}
}
}
}

void getPhoto(String photoReference) async {
var result = await this.googlePlace.photos.get(photoReference, null, 400);
var result = await this.googlePlace.photos.get(photoReference, -1, 400);
if (result != null && mounted) {
setState(() {
images.add(result);
Expand Down
Loading

0 comments on commit 26be92a

Please sign in to comment.