Flutter mapController LateInitializationError

Publicado el: 12 octubre 2024
en el canal de: Emrah KAYA
12
0

Hello everyone! I hope this video has helped solve your questions and issues. This video is shared because a solution has been found for the question/problem. I create videos for questions that have solutions. If you have any other issues, feel free to reach out to me on Instagram:   / ky.emrah  

Below, you can find the text related to the question/problem. In the video, the question will be presented first, followed by the answers. If the video moves too fast, feel free to pause and review the answers. If you need more detailed information, you can find the necessary sources and links at the bottom of this description. I hope this video has been helpful, and even if it doesn't directly solve your problem, it will guide you to the source of the solution. I'd appreciate it if you like the video and subscribe to my channel!Flutter mapController LateInitializationError

I was trying to setup a mapController to return the bounds of Flutter_map but this "LateInitializationError: Field '_internalController@' has not been initialized" error keeps coming up
import 'package:flutter/material.dart';
import 'dart:io';
import 'package:dio_cache_interceptor/dio_cache_interceptor.dart';
import 'package:dio_cache_interceptor_file_store/dio_cache_interceptor_file_store.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map_cache/flutter_map_cache.dart';
import 'package:latlong2/latlong.dart';
import 'package:path_provider/path_provider.dart';

class CityMap extends StatefulWidget {
const CityMap({super.key});

@override
State CityMap createState() = _CityMapState();
}

class _CityMapState extends State CityMap with TickerProviderStateMixin {
final Future CacheStore _cacheStoreFuture = _getCacheStore();

static Future CacheStore _getCacheStore() async {
final dir = await getTemporaryDirectory();
return FileCacheStore('${dir.path}${Platform.pathSeparator}MapTiles');
}

var _mapController = MapController();

List LatLng markertram = [const LatLng(51.502762, -0.113125)];

List Marker buildMarker() {

List Marker markers = [];
LatLngBounds bounder = _mapController.camera.visibleBounds;

void createMarkers(
List LatLng positions, String metroNumber, List String polyLineIds) {
List Marker tramMarkers = positions.where((position) {
return bounder.contains(position);
}).map((position) {
return Marker(
width: 20.0,
height: 20.0,
point: position,
rotate: true,
alignment: Alignment.center,
child: GestureDetector(
onTap: () {},
child: Tooltip(
message: " Metro $metroNumber",
child: const Icon(Icons.tram_outlined),
),
),
);
}).toList();
markers.addAll(tramMarkers);
}

createMarkers(markertram, '2', ['tram2']);

return markers;
}

@override
void initState() {
super.initState();
_mapController = MapController();
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder CacheStore (
future: _cacheStoreFuture,
builder: (context, snapshot) {
if (snapshot.hasData) {
final cacheStore = snapshot.data!;
return Stack(
children: [
FlutterMap(
mapController: MapController(),
options: const MapOptions(
initialCenter: LatLng(51.515635, -0.092354),
initialZoom: 15.5,
maxZoom: 19.5,
),
children: [
TileLayer(
urlTemplate:
'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
tileProvider: CachedTileProvider(
store: cacheStore,
),
),
MarkerLayer(
markers: buildMarker(),
),
],
),
],
);

} else if (snapshot.hasError) {
return Center(child: Text(snapsSource of the question:
https://stackoverflow.com/questions/7...

Question and source license information:
https://meta.stackexchange.com/help/l...
https://stackoverflow.com/


En esta página del sitio puede ver el video en línea Flutter mapController LateInitializationError de Duración online en buena calidad , que subió el usuario Emrah KAYA 12 octubre 2024, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 12 veces y le gustó 0 a los espectadores. Disfruta viendo!