Last active
December 17, 2021 06:37
-
-
Save RageshAntony/b6f0189d3ec06894afafd77b31bb17be to your computer and use it in GitHub Desktop.
FlutterMap GetX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
======== Exception caught by widgets library ======================================================= | |
The following message was thrown building Obx(has builder, dirty, state: _ObxState#e7ef9): | |
[Get] the improper use of a GetX has been detected. | |
You should only use GetX or Obx for the specific widget that will be updated. | |
If you are seeing this error, you probably did not insert any observable variables into GetX/Obx | |
or insert them outside the scope that GetX considers suitable for an update | |
(example: GetX => HeavyWidget => variableObservable). | |
If you need to update a parent widget and a child widget, wrap each one in an Obx/GetX. | |
The relevant error-causing widget was: | |
Obx Obx:file:///Users/sud/RageshAntony/flutter/get_sample/lib/app/modules/home/views/map_view.dart:17:12 | |
When the exception was thrown, this was the stack: | |
#0 RxInterface.notifyChildren (package:get/get_rx/src/rx_types/rx_core/rx_interface.dart:29:7) | |
#1 _ObxState.build (package:get/get_state_manager/src/rx_flutter/rx_obx_widget.dart:54:19) | |
#2 StatefulElement.build (package:flutter/src/widgets/framework.dart:4782:27) | |
#3 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4665:15) | |
#4 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4840:11) | |
#5 Element.rebuild (package:flutter/src/widgets/framework.dart:4355:5) | |
#6 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4643:5) | |
#7 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4831:11) | |
#8 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4638:5) | |
... Normal element mounting (7 frames) | |
#15 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3673:14) | |
#16 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6333:36) | |
#17 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6344:32) | |
... Normal element mounting (34 frames) | |
#51 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3673:14) | |
#52 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6333:36) | |
#53 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6344:32) | |
... Normal element mounting (259 frames) | |
#312 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3673:14) | |
#313 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6333:36) | |
#314 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6344:32) | |
... Normal element mounting (397 frames) | |
#711 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3673:14) | |
#712 Element.updateChild (package:flutter/src/widgets/framework.dart:3425:18) | |
#713 RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1198:16) | |
#714 RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1167:5) | |
#715 RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1112:18) | |
#716 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2573:19) | |
#717 RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1111:13) | |
#718 WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:944:7) | |
#719 WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:924:7) | |
(elided 11 frames from class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch) | |
==================================================================================================== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'dart:convert'; | |
import 'package:flutter/cupertino.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_map/flutter_map.dart'; | |
import 'package:get/get.dart'; | |
import 'package:latlong2/latlong.dart'; | |
import 'package:open_route_service/open_route_service.dart'; | |
class HomeController extends GetxController { | |
//TODO: Implement HomeController | |
final count = 0.obs; | |
late Timer timer; | |
var startPlace = "".obs; | |
final startPlaceController = TextEditingController(); | |
var markers = <Marker>[ | |
Marker( | |
width: 20.0, | |
height: 20.0, | |
point: LatLng(9.872797, 78.17801), | |
builder: (ctx) => Container( | |
child: FlutterLogo( | |
textColor: Colors.blue, | |
key: ObjectKey(Colors.blue), | |
), | |
), | |
) | |
].obs; | |
RxList<LatLng> routePoints = <LatLng>[ | |
LatLng(51.5, -0.09), | |
LatLng(53.3498, -6.2603), | |
LatLng(48.8566, 2.3522), | |
].obs; | |
late OpenRouteService client; | |
@override | |
void onInit() { | |
super.onInit(); | |
timer = Timer.periodic(Duration(seconds: 1), (Timer t) => increment()); | |
} | |
@override | |
void onReady() { | |
super.onReady(); | |
} | |
@override | |
void onClose() { | |
timer?.cancel(); | |
} | |
void increment() => count.value++; | |
void getPlace(double startLat,double startLng) async { | |
final coding = await client.geocodeReverseGet(point: ORSCoordinate(latitude: startLat, longitude: startLng)); | |
startPlace.value = coding.features[0].properties["county"]; | |
startPlaceController.text = startPlace.value; | |
} | |
void getRoute(double startLat,double startLng,double endLat,double endLng) async { | |
client = OpenRouteService(apiKey: '5b3ce3597851110001cf6248257dc61807394db7a9fe1700ca213cc3'); | |
print ("routing }"); | |
// Form Route between coordinates | |
final List<ORSCoordinate> routeCoordinates = await client.directionsRouteCoordsGet( | |
startCoordinate: ORSCoordinate(latitude: startLat, longitude: startLng), | |
endCoordinate: ORSCoordinate(latitude: endLat, longitude: endLng), | |
profileOverride: ORSProfile.drivingCar | |
); | |
print ("routing end"); | |
final coding = await client.geocodeReverseGet(point: ORSCoordinate(latitude: startLat, longitude: startLng)); | |
startPlace.value = coding.features[0].properties["county"]; | |
startPlaceController.text = startPlace.value; | |
print("geocoding ${jsonEncode( coding.features[0].properties)}"); | |
// Print the route coordinates | |
// routeCoordinates.forEach(print); | |
// Map route coordinates to a list of LatLng (requires google_maps_flutter package) | |
// to be used in the Map route Polyline. | |
routePoints.value = routeCoordinates | |
.map((coordinate) => LatLng(coordinate.latitude, coordinate.longitude)) | |
.toList(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_map/flutter_map.dart'; | |
import 'package:get/get.dart'; | |
import 'package:get_sample/app/modules/home/views/map_view.dart'; | |
import 'package:latlong2/latlong.dart'; | |
import 'package:open_route_service/open_route_service.dart'; | |
import '../controllers/home_controller.dart'; | |
class HomeView extends GetView<HomeController> { | |
@override | |
Widget build(BuildContext context) { | |
controller.getRoute(9.8753750, 78.1785170, 9.9545490, 78.1805230); | |
return Scaffold( | |
body: SafeArea( | |
child: Stack( | |
children: [ | |
MapView(), // throwing error | |
Align( | |
alignment: Alignment.topCenter, | |
child: TextFormField( | |
controller: controller.startPlaceController, | |
decoration: InputDecoration( | |
labelText: 'Label text', | |
errorText: 'Error message', | |
border: OutlineInputBorder(), | |
suffixIcon: Icon( | |
Icons.error, | |
), | |
), | |
)) | |
], | |
), | |
), | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:get/get.dart'; | |
import 'app/routes/app_pages.dart'; | |
void main() { | |
runApp( | |
GetMaterialApp( | |
title: "Application", | |
initialRoute: AppPages.INITIAL, | |
getPages: AppPages.routes, | |
), | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_map/flutter_map.dart'; | |
import 'package:get/get.dart'; | |
import 'package:latlong2/latlong.dart'; | |
import 'package:open_route_service/open_route_service.dart'; | |
import '../controllers/home_controller.dart'; | |
class MapView extends GetView<HomeController> { | |
@override | |
Widget build(BuildContext context) { | |
return Obx( () => FlutterMap( // throwing error | |
options: MapOptions( | |
center: LatLng(9.8753750, 78.1785170), | |
zoom: 15.0, | |
onLongPress: (pos,lat) { | |
print ("LATLANG ${lat.toString()}"); | |
print ("LATLANG ${pos.global.distance}"); | |
controller.markers.add(Marker( | |
width: 20.0, | |
height: 20.0, | |
point: lat, | |
builder: (ctx) => Container( | |
child: FlutterLogo( | |
textColor: Colors.blue, | |
key: ObjectKey(Colors.blue), | |
), | |
), | |
)); | |
controller.markers.refresh(); | |
} | |
), | |
layers: [ | |
TileLayerOptions( | |
urlTemplate: | |
'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', | |
subdomains: ['a', 'b', 'c'], | |
// For example purposes. It is recommended to use | |
// TileProvider with a caching and retry strategy, like | |
// NetworkTileProvider or CachedNetworkTileProvider | |
tileProvider: NonCachingNetworkTileProvider(), | |
), | |
MarkerLayerOptions(markers: controller.markers), // obs variable | |
PolylineLayerOptions( | |
polylines: [ | |
Polyline( | |
points: controller.routePoints, // obs variable | |
strokeWidth: 4.0, | |
gradientColors: [ | |
Color(0xffE40203), | |
Color(0xffFEED00), | |
Color(0xff007E2D), | |
]), | |
], | |
), | |
], | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment