como andan? estoy trabajando con Google Maps, en esta oportunidad, me encuentro con el siguiente error: (Provider)
[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Error: Could not find the correct Provider above this GoogleMaps Widget
Y la clase con la que estoy trabajando es la siguiente: (una parte, para no hacerla larga):
import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:provider/provider.dart';
import 'DirectionProvider.dart';
import 'package:geolocator/geolocator.dart';
class GoogleMaps extends StatefulWidget {
final String x, y;
GoogleMaps(this.x, this.y);
final LatLng fromPoint = LatLng(-37.020125, -62.409388);
static ChangeNotifierProvider init(double x, double y) => ChangeNotifierProvider(
create: (_) => new DirectionProvider(),
);
@override
_GoogleMapsState createState() => _GoogleMapsState();
}
class _GoogleMapsState extends State<GoogleMaps> {
Position _currentPosition;
LatLng toPoint;
void initState() {
_getCurrentLocation();
var x2 = double.parse(widget.x);
var y2 = double.parse(widget.y);
toPoint = new LatLng(x2, y2);
super.initState();
}
GoogleMapController _mapController;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Google Maps'),
),
body: Consumer<DirectionProvider>(
builder: (_,api,__) {
return GoogleMap(
initialCameraPosition: CameraPosition(
target: widget.fromPoint,
zoom: 12,
),
markers: _createMarkers(),
polylines: api.currentRoute,
onMapCreated: _onMapCreated,
myLocationEnabled: true,
myLocationButtonEnabled: true,
);
},
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.zoom_out_map),
onPressed: _centerView,
),
);
}
Set<Marker> _createMarkers() {
var tmp = Set<Marker>();
tmp.add(Marker(
markerId: MarkerId("FromPoint"),
position: widget.fromPoint,
infoWindow: InfoWindow(title: "estoy aca"),
));
tmp.add(Marker(
markerId: MarkerId("toPoint"),
position: toPoint,
infoWindow: InfoWindow(title: "llegada"),
));
return tmp;
}
void _onMapCreated(GoogleMapController controller) {
_mapController = controller;
_centerView();
}
_centerView() async {
var api = Provider.of<DirectionProvider>(context);
await _mapController.getVisibleRegion();
print("buscando direcciones");
await api.findDirections(widget.fromPoint, toPoint);
var left = min(
widget.fromPoint.latitude, toPoint.latitude); //SE CALCULA COORDENADAS.
var right = max(widget.fromPoint.latitude, toPoint.latitude);
var top = max(widget.fromPoint.longitude, toPoint.longitude);
var bottom = min(widget.fromPoint.longitude, toPoint.longitude);
api.currentRoute.first.points.forEach((point) {
left = min(left, point.latitude);
right = max(right, point.latitude);
top = max(top, point.longitude);
bottom = min(bottom, point.longitude);
});
var bounds = LatLngBounds(
southwest: LatLng(left, bottom),
northeast: LatLng(right, top),
);
var cameraUpdate = CameraUpdate.newLatLngBounds(bounds, 50);
_mapController.animateCamera(cameraUpdate);
}
_getCurrentLocation() {
var geolocator = Geolocator();
var locationOptions = LocationOptions(accuracy: LocationAccuracy.high, distanceFilter: 10);
StreamSubscription<Position> positionStream = geolocator.getPositionStream(locationOptions).listen((Position position) {
print(position == null ? 'Unknown' : position.latitude.toString() + ', ' + position.longitude.toString());
});
}
}
No puedo encontrar, sinceramente el error. Muchas gracias de antemano. Ya Ejecute Flutter Clean. También esta la clase DirectionProvider, pero en ésta, simplemente esta al api key y se calcula cual es el mejor camino para llegar a destino.
(se actualizo)