UPDATED
El problema que tienes es por el contexto como lo dije anteriormente, estás usando Provider.of con un contexto local, para solucionar el problema la manera más adecuada es sacar el ChangeNotifierProvider y ponerlo arriba, ahora para llamar al widget de google maps, ya no haces esto:
GoogleMaps(123, 456)
sino esto
GoogleMaps.init(123,456)
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);
static ChangeNotifierProvider init(double x, double y) => ChangeNotifierProvider(
create: (_) => new DirectionProvider(),
child; GoogleMaps(x,y);
);
final LatLng fromPoint = LatLng(-37.020125, -62.409388);
@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: (BuildContext context, DirectionProvider api,
Widget child) {
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());
});
}
}
Te recomiendo que repases bien el concepto de StatefulWidget antes de pasar a Provider, y también te paso el link de un canal de youtube que tengo junto con un amigo de Mexico, donde hablamos sobre State Management: https://www.youtube.com/c/TheDartSide