asdu4

Maps

Jan 9th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.10 KB | None | 0 0
  1. /*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?
  2. Here is my code.
  3. */
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter_google_places/flutter_google_places.dart';
  6. import 'package:google_maps_flutter/google_maps_flutter.dart';
  7. import 'dart:async';
  8. import 'package:google_maps_webservice/places.dart';
  9. import 'package:geocoder/geocoder.dart';
  10.  
  11. void main() => runApp(MyApp());
  12.  
  13. const kGoogleApiKey = "My API key";
  14.  
  15. GoogleMapsPlaces _places = GoogleMapsPlaces(apiKey: kGoogleApiKey);
  16.  
  17. class MyApp extends StatefulWidget {
  18. @override
  19. _MyAppState createState() => _MyAppState();
  20. }
  21.  
  22. Future<Null> displayPrediction(Prediction p) async {
  23. if (p != null) {
  24. PlacesDetailsResponse detail = await _places.getDetailsByPlaceId(p.placeId);
  25.  
  26. var placeId = p.placeId;
  27. double lat = detail.result.geometry.location.lat;
  28. double lng = detail.result.geometry.location.lng;
  29.  
  30. var address = await Geocoder.local.findAddressesFromQuery(p.description);
  31.  
  32. print(lat);
  33. print(lng);
  34. }
  35. }
  36.  
  37. class _MyAppState extends State<MyApp> {
  38. GoogleMapController mapController;
  39.  
  40. final LatLng _center = const LatLng(45.521563, -122.677433);
  41.  
  42. void _onMapCreated(GoogleMapController controller) {
  43. mapController = controller;
  44. }
  45.  
  46. @override
  47. Widget build(BuildContext context) {
  48. return MaterialApp(
  49. home: Scaffold(
  50. drawer: Drawer(
  51. child: ListView(
  52. padding: EdgeInsets.only(right: 10),
  53. children: <Widget>[
  54. DrawerHeader(
  55. padding: EdgeInsets.all(30),
  56. child: Column(
  57. mainAxisAlignment: MainAxisAlignment.center,
  58. crossAxisAlignment: CrossAxisAlignment.start,
  59. children: [
  60. CircleAvatar(
  61. backgroundImage: new NetworkImage(
  62. 'https://1.bp.blogspot.com/-xkOa184EX9w/XVhTRa2WRQI/AAAAAAAAb1E/jPDX8jZ8mAIsqqNEDgb8lmfKbttdP1BDACLcBGAs/w680/Profile-Picture-For-Boys%2B%252812%2529.jpg'),
  63. ),
  64. Text(
  65. "Good morning,",
  66. style: TextStyle(color: Colors.grey, fontSize: 16.0),
  67. ),
  68. Text(
  69. "Avi",
  70. style: TextStyle(
  71. color: Colors.grey,
  72. fontSize: 22.0,
  73. fontWeight: FontWeight.bold),
  74. )
  75. ],
  76. ),
  77. ),
  78. ListTile(
  79. title: Text(
  80. 'Payment History',
  81. style: TextStyle(fontSize: 20),
  82. ),
  83. onTap: () {
  84. Navigator.pop(context);
  85. },
  86. ),
  87. ListTile(
  88. title: Text(
  89. 'Ride History',
  90. style: TextStyle(fontSize: 20),
  91. ),
  92. onTap: () {
  93. Navigator.pop(context);
  94. },
  95. ),
  96. ListTile(
  97. title: Text(
  98. 'Invite Friends',
  99. style: TextStyle(fontSize: 20),
  100. ),
  101. onTap: () {
  102. Navigator.pop(context);
  103. },
  104. ),
  105. ListTile(
  106. title: Text(
  107. 'Promo Codes',
  108. style: TextStyle(fontSize: 20),
  109. ),
  110. onTap: () {
  111. Navigator.pop(context);
  112. },
  113. ),
  114. ListTile(
  115. title: Text(
  116. 'Settings',
  117. style: TextStyle(fontSize: 20),
  118. ),
  119. onTap: () {
  120. Navigator.pop(context);
  121. },
  122. ),
  123. ListTile(
  124. title: Text(
  125. 'Support',
  126. style: TextStyle(fontSize: 20),
  127. ),
  128. onTap: () {
  129. Navigator.pop(context);
  130. },
  131. ),
  132. ListTile(
  133. title: Text(
  134. 'Log Out',
  135. style: TextStyle(fontSize: 20),
  136. ),
  137. onTap: () {
  138. Navigator.pop(context);
  139. },
  140. ),
  141. ],
  142. ),
  143. ),
  144. appBar: PreferredSize(
  145. preferredSize: Size.fromHeight(70),
  146. child: AppBar(
  147. backgroundColor: Colors.transparent,
  148. elevation: 0,
  149. title: Column(
  150. mainAxisAlignment: MainAxisAlignment.center,
  151. crossAxisAlignment: CrossAxisAlignment.start,
  152. children: [
  153. Text(
  154. "Good morning, Avi",
  155. style: TextStyle(color: Colors.grey, fontSize: 16.0),
  156. ),
  157. Text(
  158. "Where are you going?",
  159. style: TextStyle(
  160. color: Colors.grey,
  161. fontSize: 22.0,
  162. fontWeight: FontWeight.bold),
  163. )
  164. ],
  165. ),
  166. leading: new Padding(
  167. padding: const EdgeInsets.all(5.0),
  168. child: new CircleAvatar(
  169. backgroundImage: new NetworkImage(
  170. 'https://1.bp.blogspot.com/-xkOa184EX9w/XVhTRa2WRQI/AAAAAAAAb1E/jPDX8jZ8mAIsqqNEDgb8lmfKbttdP1BDACLcBGAs/w680/Profile-Picture-For-Boys%2B%252812%2529.jpg'),
  171. ),
  172. ),
  173. ),
  174. ),
  175. body: Stack(
  176. children: <Widget>[
  177. GoogleMap(
  178. onMapCreated: _onMapCreated,
  179. initialCameraPosition: CameraPosition(
  180. target: _center,
  181. zoom: 15.0,
  182. ),
  183. ),
  184. Positioned(
  185. top: 10,
  186. right: 20,
  187. left: 20,
  188. child: Container(
  189. color: Colors.white,
  190. child: Row(
  191. children: <Widget>[
  192. Expanded(
  193. child: TextField(
  194. cursorColor: Colors.black,
  195. keyboardType: TextInputType.text,
  196. textInputAction: TextInputAction.go,
  197. decoration: InputDecoration(
  198. border: new OutlineInputBorder(
  199. borderRadius: const BorderRadius.all(
  200. const Radius.circular(10),
  201. ),
  202. ),
  203. suffixIcon: IconButton(
  204. //search icon
  205. splashColor: Colors.grey,
  206. icon: Icon(
  207. Icons.search,
  208. color: Colors.grey,
  209. ),
  210. onPressed: () async {
  211. Prediction p = await PlacesAutocomplete.show(
  212. context: context, apiKey: kGoogleApiKey);
  213. displayPrediction(p);
  214. },
  215. ),
  216. contentPadding: EdgeInsets.symmetric(horizontal: 20),
  217. hintText: "Search for destination",
  218. ),
  219. ),
  220. ),
  221. ],
  222. ),
  223. ),
  224. ),
  225. Stack(
  226. children: <Widget>[
  227. Positioned(
  228. top: 500,
  229. left: 320,
  230. child: Container(
  231. decoration: new BoxDecoration(
  232. shape: BoxShape.circle,
  233. color: Colors.green,
  234. ),
  235. child: IconButton(
  236. icon: Icon(
  237. Icons.gps_fixed,
  238. color: Colors.white,
  239. ),
  240. onPressed: () {},
  241. )),
  242. ),
  243. ],
  244. )
  245. ],
  246. ),
  247. ),
  248. );
  249. }
  250. }
  251.  
  252. /* AFTER HOT RELOAD I GET THIS IN TERMINAL
  253.  
  254. Performing hot reload...
  255.  
  256. Reloaded 1 of 671 libraries in 2,326ms.
  257. 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.
  258. 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.
  259. E/flutter (10213): #0 Navigator.of.<anonymous closure> (package:flutter/src/widgets/navigator.dart:1495:9)
  260. E/flutter (10213): #1 Navigator.of (package:flutter/src/widgets/navigator.dart:1502:6)
  261. E/flutter (10213): #2 Navigator.push (package:flutter/src/widgets/navigator.dart:1121:22)
  262. E/flutter (10213): #3 PlacesAutocomplete.show (package:flutter_google_places/src/flutter_google_places.dart:467:22)
  263. E/flutter (10213): #4 _MyAppState.build.<anonymous closure> (package:home/main.dart:211:71)
  264. E/flutter (10213): #5 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:706:14)
  265. E/flutter (10213): #6 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:789:36)
  266. E/flutter (10213): #7 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
  267. E/flutter (10213): #8 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:486:11)
  268. E/flutter (10213): #9 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:264:5)
  269. E/flutter (10213): #10 BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:236:7)
  270. E/flutter (10213): #11 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
  271. E/flutter (10213): #12 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:222:20)
  272. E/flutter (10213): #13 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
  273. E/flutter (10213): #14 GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
  274. E/flutter (10213): #15 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
  275. E/flutter (10213): #16 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
  276. E/flutter (10213): #17 _rootRunUnary (dart:async/zone.dart:1138:13)
  277. E/flutter (10213): #18 _CustomZone.runUnary (dart:async/zone.dart:1031:19)
  278. E/flutter (10213): #19 _CustomZone.runUnaryGuarded (dart:async/zone.dart:933:7)
  279. E/flutter (10213): #20 _invoke1 (dart:ui/hooks.dart:273:10)
  280. E/flutter (10213): #21 _dispatchPointerDataPacket (dart:ui/hooks.dart:182:5)
  281. E/flutter (10213):
  282.  
  283. */
Add Comment
Please, Sign In to add comment