Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

You updated plugin to null-safety, but didn't update example, example doesn't work #22

Open
unknowndev00 opened this issue Apr 19, 2021 · 3 comments

Comments

@unknowndev00
Copy link

unknowndev00 commented Apr 19, 2021

You updated the plugin to null-safety, but didn't update the example.

@jtkeyva
Copy link

jtkeyva commented Apr 24, 2021

+1

@jtkeyva
Copy link

jtkeyva commented Apr 24, 2021

@unknowndev00 I migrated the example code, but had to disable DotEnv. Here you go:

`import 'dart:typed_data';

import 'package:flutter/material.dart';
// import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:google_place/google_place.dart';

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

class MyApp extends StatelessWidget {
@OverRide
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
);
}
}

class HomePage extends StatefulWidget {
@OverRide
_HomePageState createState() => _HomePageState();
}

class _HomePageState extends State {
GooglePlace? googlePlace;
List? predictions = [];

@OverRide
void initState() {
// String apiKey = DotEnv().env['API_KEY'];
String apiKey = "myKey";
googlePlace = GooglePlace(apiKey);
super.initState();
}

@OverRide
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Container(
margin: EdgeInsets.only(right: 20, left: 20, top: 20),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextField(
decoration: InputDecoration(
labelText: "Search",
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.blue,
width: 2.0,
),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.black54,
width: 2.0,
),
),
),
onChanged: (value) {
if (value.isNotEmpty) {
autoCompleteSearch(value);
} else {
if (predictions!.length > 0 && mounted) {
setState(() {
predictions = [];
});
}
}
},
),
SizedBox(
height: 10,
),
Expanded(
child: ListView.builder(
itemCount: predictions!.length,
itemBuilder: (context, index) {
return ListTile(
leading: CircleAvatar(
child: Icon(
Icons.pin_drop,
color: Colors.white,
),
),
title: Text(predictions![index].description!),
onTap: () {
debugPrint(predictions![index].placeId);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailsPage(
placeId: predictions![index].placeId,
googlePlace: googlePlace,
),
),
);
},
);
},
),
),
Container(
margin: EdgeInsets.only(top: 10, bottom: 10),
child: Image.asset("assets/powered_by_google.png"),
),
],
),
),
),
);
}

void autoCompleteSearch(String value) async {
var result = await googlePlace!.autocomplete.get(value);
if (result != null && result.predictions != null && mounted) {
setState(() {
predictions = result.predictions;
});
}
}
}

class DetailsPage extends StatefulWidget {
final String? placeId;
final GooglePlace? googlePlace;

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

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

class _DetailsPageState extends State {
final String? placeId;
final GooglePlace? googlePlace;

_DetailsPageState(this.placeId, this.googlePlace);

DetailsResult? detailsResult;
List images = [];

@OverRide
void initState() {
getDetils(this.placeId!);
super.initState();
}

@OverRide
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Details"),
backgroundColor: Colors.blueAccent,
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.blueAccent,
onPressed: () {
getDetils(this.placeId!);
},
child: Icon(Icons.refresh),
),
body: SafeArea(
child: Container(
margin: EdgeInsets.only(right: 20, left: 20, top: 20),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
height: 200,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: images.length,
itemBuilder: (context, index) {
return Container(
width: 250,
child: Card(
elevation: 4,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
child: ClipRRect(
borderRadius: BorderRadius.circular(10.0),
child: Image.memory(
images[index],
fit: BoxFit.fill,
),
),
),
);
},
),
),
SizedBox(
height: 10,
),
Expanded(
child: Card(
elevation: 4,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
child: ListView(
children: [
Container(
margin: EdgeInsets.only(left: 15, top: 10),
child: Text(
"Details",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
),
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,
itemBuilder: (context, index) {
return Container(
margin: EdgeInsets.only(right: 10),
child: Chip(
label: Text(
detailsResult!.types![index],
style: TextStyle(
color: Colors.white,
),
),
backgroundColor: Colors.blueAccent,
),
);
},
),
)
: Container(),
Container(
margin: EdgeInsets.only(left: 15, top: 10),
child: ListTile(
leading: CircleAvatar(
child: Icon(Icons.location_on),
),
title: Text(
detailsResult != null &&
detailsResult!.formattedAddress != null
? 'Address: ${detailsResult!.formattedAddress}'
: "Address: null",
),
),
),
Container(
margin: EdgeInsets.only(left: 15, top: 10),
child: ListTile(
leading: CircleAvatar(
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()}'
: "Geometry: null",
),
),
),
Container(
margin: EdgeInsets.only(left: 15, top: 10),
child: ListTile(
leading: CircleAvatar(
child: Icon(Icons.timelapse),
),
title: Text(
detailsResult != null &&
detailsResult!.utcOffset != null
? 'UTC offset: ${detailsResult!.utcOffset.toString()} min'
: "UTC offset: null",
),
),
),
Container(
margin: EdgeInsets.only(left: 15, top: 10),
child: ListTile(
leading: CircleAvatar(
child: Icon(Icons.rate_review),
),
title: Text(
detailsResult != null &&
detailsResult!.rating != null
? 'Rating: ${detailsResult!.rating.toString()}'
: "Rating: null",
),
),
),
Container(
margin: EdgeInsets.only(left: 15, top: 10),
child: ListTile(
leading: CircleAvatar(
child: Icon(Icons.attach_money),
),
title: Text(
detailsResult != null &&
detailsResult!.priceLevel != null
? 'Price level: ${detailsResult!.priceLevel.toString()}'
: "Price level: null",
),
),
),
],
),
),
),
Container(
margin: EdgeInsets.only(top: 20, bottom: 10),
child: Image.asset("assets/powered_by_google.png"),
),
],
),
),
),
);
}

void getDetils(String placeId) async {
var result = await this.googlePlace!.details.get(placeId);
if (result != null && result.result != null && mounted) {
setState(() {
detailsResult = result.result;
images = [];
});

  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, 0, 400);
if (result != null && mounted) {
setState(() {
images.add(result);
});
}
}
}
`

@ronyef
Copy link

ronyef commented Jul 13, 2021

shrinkWrap: true in ListView.builder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants