Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*I am trying to create a google places search bar but somehow caught in some exception which i have mentioned at the end of the code. How can I solve it?
- Here is my code.
- */
- import 'package:flutter/material.dart';
- import 'package:flutter_google_places/flutter_google_places.dart';
- import 'package:google_maps_flutter/google_maps_flutter.dart';
- import 'dart:async';
- import 'package:google_maps_webservice/places.dart';
- import 'package:geocoder/geocoder.dart';
- void main() => runApp(MyApp());
- const kGoogleApiKey = "My API key";
- GoogleMapsPlaces _places = GoogleMapsPlaces(apiKey: kGoogleApiKey);
- class MyApp extends StatefulWidget {
- @override
- _MyAppState createState() => _MyAppState();
- }
- Future<Null> displayPrediction(Prediction p) async {
- if (p != null) {
- PlacesDetailsResponse detail = await _places.getDetailsByPlaceId(p.placeId);
- var placeId = p.placeId;
- double lat = detail.result.geometry.location.lat;
- double lng = detail.result.geometry.location.lng;
- var address = await Geocoder.local.findAddressesFromQuery(p.description);
- print(lat);
- print(lng);
- }
- }
- class _MyAppState extends State<MyApp> {
- GoogleMapController mapController;
- final LatLng _center = const LatLng(45.521563, -122.677433);
- void _onMapCreated(GoogleMapController controller) {
- mapController = controller;
- }
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- home: Scaffold(
- drawer: Drawer(
- child: ListView(
- padding: EdgeInsets.only(right: 10),
- children: <Widget>[
- DrawerHeader(
- padding: EdgeInsets.all(30),
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- CircleAvatar(
- backgroundImage: new NetworkImage(
- 'https://1.bp.blogspot.com/-xkOa184EX9w/XVhTRa2WRQI/AAAAAAAAb1E/jPDX8jZ8mAIsqqNEDgb8lmfKbttdP1BDACLcBGAs/w680/Profile-Picture-For-Boys%2B%252812%2529.jpg'),
- ),
- Text(
- "Good morning,",
- style: TextStyle(color: Colors.grey, fontSize: 16.0),
- ),
- Text(
- "Avi",
- style: TextStyle(
- color: Colors.grey,
- fontSize: 22.0,
- fontWeight: FontWeight.bold),
- )
- ],
- ),
- ),
- ListTile(
- title: Text(
- 'Payment History',
- style: TextStyle(fontSize: 20),
- ),
- onTap: () {
- Navigator.pop(context);
- },
- ),
- ListTile(
- title: Text(
- 'Ride History',
- style: TextStyle(fontSize: 20),
- ),
- onTap: () {
- Navigator.pop(context);
- },
- ),
- ListTile(
- title: Text(
- 'Invite Friends',
- style: TextStyle(fontSize: 20),
- ),
- onTap: () {
- Navigator.pop(context);
- },
- ),
- ListTile(
- title: Text(
- 'Promo Codes',
- style: TextStyle(fontSize: 20),
- ),
- onTap: () {
- Navigator.pop(context);
- },
- ),
- ListTile(
- title: Text(
- 'Settings',
- style: TextStyle(fontSize: 20),
- ),
- onTap: () {
- Navigator.pop(context);
- },
- ),
- ListTile(
- title: Text(
- 'Support',
- style: TextStyle(fontSize: 20),
- ),
- onTap: () {
- Navigator.pop(context);
- },
- ),
- ListTile(
- title: Text(
- 'Log Out',
- style: TextStyle(fontSize: 20),
- ),
- onTap: () {
- Navigator.pop(context);
- },
- ),
- ],
- ),
- ),
- appBar: PreferredSize(
- preferredSize: Size.fromHeight(70),
- child: AppBar(
- backgroundColor: Colors.transparent,
- elevation: 0,
- title: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Text(
- "Good morning, Avi",
- style: TextStyle(color: Colors.grey, fontSize: 16.0),
- ),
- Text(
- "Where are you going?",
- style: TextStyle(
- color: Colors.grey,
- fontSize: 22.0,
- fontWeight: FontWeight.bold),
- )
- ],
- ),
- leading: new Padding(
- padding: const EdgeInsets.all(5.0),
- child: new CircleAvatar(
- backgroundImage: new NetworkImage(
- 'https://1.bp.blogspot.com/-xkOa184EX9w/XVhTRa2WRQI/AAAAAAAAb1E/jPDX8jZ8mAIsqqNEDgb8lmfKbttdP1BDACLcBGAs/w680/Profile-Picture-For-Boys%2B%252812%2529.jpg'),
- ),
- ),
- ),
- ),
- body: Stack(
- children: <Widget>[
- GoogleMap(
- onMapCreated: _onMapCreated,
- initialCameraPosition: CameraPosition(
- target: _center,
- zoom: 15.0,
- ),
- ),
- Positioned(
- top: 10,
- right: 20,
- left: 20,
- child: Container(
- color: Colors.white,
- child: Row(
- children: <Widget>[
- Expanded(
- child: TextField(
- cursorColor: Colors.black,
- keyboardType: TextInputType.text,
- textInputAction: TextInputAction.go,
- decoration: InputDecoration(
- border: new OutlineInputBorder(
- borderRadius: const BorderRadius.all(
- const Radius.circular(10),
- ),
- ),
- suffixIcon: IconButton(
- //search icon
- splashColor: Colors.grey,
- icon: Icon(
- Icons.search,
- color: Colors.grey,
- ),
- onPressed: () async {
- Prediction p = await PlacesAutocomplete.show(
- context: context, apiKey: kGoogleApiKey);
- displayPrediction(p);
- },
- ),
- contentPadding: EdgeInsets.symmetric(horizontal: 20),
- hintText: "Search for destination",
- ),
- ),
- ),
- ],
- ),
- ),
- ),
- Stack(
- children: <Widget>[
- Positioned(
- top: 500,
- left: 320,
- child: Container(
- decoration: new BoxDecoration(
- shape: BoxShape.circle,
- color: Colors.green,
- ),
- child: IconButton(
- icon: Icon(
- Icons.gps_fixed,
- color: Colors.white,
- ),
- onPressed: () {},
- )),
- ),
- ],
- )
- ],
- ),
- ),
- );
- }
- }
- /* AFTER HOT RELOAD I GET THIS IN TERMINAL
- Performing hot reload...
- Reloaded 1 of 671 libraries in 2,326ms.
- E/flutter (10213): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Navigator operation requested with a context that does not include a Navigator.
- E/flutter (10213): The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.
- E/flutter (10213): #0 Navigator.of.<anonymous closure> (package:flutter/src/widgets/navigator.dart:1495:9)
- E/flutter (10213): #1 Navigator.of (package:flutter/src/widgets/navigator.dart:1502:6)
- E/flutter (10213): #2 Navigator.push (package:flutter/src/widgets/navigator.dart:1121:22)
- E/flutter (10213): #3 PlacesAutocomplete.show (package:flutter_google_places/src/flutter_google_places.dart:467:22)
- E/flutter (10213): #4 _MyAppState.build.<anonymous closure> (package:home/main.dart:211:71)
- E/flutter (10213): #5 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:706:14)
- E/flutter (10213): #6 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:789:36)
- E/flutter (10213): #7 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
- E/flutter (10213): #8 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:486:11)
- E/flutter (10213): #9 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:264:5)
- E/flutter (10213): #10 BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:236:7)
- E/flutter (10213): #11 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
- E/flutter (10213): #12 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:222:20)
- E/flutter (10213): #13 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
- E/flutter (10213): #14 GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
- E/flutter (10213): #15 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
- E/flutter (10213): #16 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
- E/flutter (10213): #17 _rootRunUnary (dart:async/zone.dart:1138:13)
- E/flutter (10213): #18 _CustomZone.runUnary (dart:async/zone.dart:1031:19)
- E/flutter (10213): #19 _CustomZone.runUnaryGuarded (dart:async/zone.dart:933:7)
- E/flutter (10213): #20 _invoke1 (dart:ui/hooks.dart:273:10)
- E/flutter (10213): #21 _dispatchPointerDataPacket (dart:ui/hooks.dart:182:5)
- E/flutter (10213):
- */
Add Comment
Please, Sign In to add comment