-
-
Notifications
You must be signed in to change notification settings - Fork 860
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LateInitializationError for MapState in map.dart #926
Comments
Manually tested all features in the example app without hitting this error. So I don't think it is a bug that is hit often? |
I keep encountering this problem too |
I confirm, this problem also constantly arises
|
Can anyone do a minimal example to reproduce ? It would be good to know what circumstances create it. |
Is this happening after navigation return or something, and the
mapController isn't getting reset whilst the map is maybe ?
…On Wed, Jun 9, 2021 at 3:03 PM Aleksey Sannikov ***@***.***> wrote:
Can anyone do a minimal example to reproduce ? It would be good to know
what circumstances create it.
I have a problem when I pass the mapController, if not pass it - no
problem.
The error occurs when assigning state to mapController (flutter_map-0.13.1
/ lib / src / map / flutter_map_state.dart: 41)
Screen of data at the first entry (no errors):
[image: image]
<https://user-images.githubusercontent.com/22722008/121369024-91751900-c94c-11eb-82da-5b0e6d26dbd6.png>
Screen of data on subsequent visits, after this error:
[image: image]
<https://user-images.githubusercontent.com/22722008/121369135-a6ea4300-c94c-11eb-8d70-99acf340f127.png>
stack:
#0 LateError._throwFieldAlreadyInitialized (dart:_internal-patch/internal_patch.dart:203:5)
#1 MapControllerImpl._state= (package:flutter_map/src/map/map.dart:16:23)
#2 MapControllerImpl.state= (package:flutter_map/src/map/map.dart:26:5)
#3 FlutterMapState.initState (package:flutter_map/src/map/flutter_map_state.dart:41:19)
#4 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4711:57)
#5 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4548:5)
#6 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14)
#7 Element.updateChild (package:flutter/src/widgets/framework.dart:3360:20)
#8 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4599:16)
#9 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4746:11)
#10 Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5)
#11 StatefulElement.update (package:flutter/src/widgets/framework.dart:4778:5)
#12 Element.updateChild (package:flutter/src/widgets/framework.dart:3350:15)
#13 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4599:16)
#14 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4746:11)
#15 Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5)
#16 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2582:33)
#17 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:875:21)
#18 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:328:5)
#19 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1144:15)
#20 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1082:9)
#21 SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:998:5)
#22 _rootRun (dart:async/zone.dart:1354:13)
#23 _CustomZone.run (dart:async/zone.dart:1258:19)
#24 _CustomZone.runGuarded (dart:async/zone.dart:1162:7)
#25 _invoke (dart:ui/hooks.dart:163:10)
#26 PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:259:5)
#27 _drawFrame (dart:ui/hooks.dart:126:31)
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#926 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AA5YN5IN5ALYDGVSILBHJCTTR5YDTANCNFSM46JL6QUA>
.
|
@ibrierley in my case it happens after removing the marker (for further rendering of the new marker), which shows the current location I redraw the map constantly, when new geolocation data comes in, it turns out that the layers are just updating
|
That sounds weird, well, as mentioned, if someone can produce a test case to highlight it (so it can be known if its fixed or not), it would be useful. |
This happens when you pass a My case is something like this: FlutterMap(
key: ValueKey(MediaQuery.of(context).orientation),
mapController: mapController,
) The |
In my case, I found the problem. I got a problem when I was adding a route (updated layers). Before adding the route, I called the CircularProgressIndicator display, thereby destroying the FlitterMap, and then re-creating the map. Perhaps I created a new map until the previous one had time to pass the destruction, but this is just an assumption. As a result, I think that this is my problem. |
@jexme In your case the widget tree changes probably also lead to the state being re-created. |
Any news on this issue? I'm stuck with null safety update and this bug. |
Can you post a minimal example highlighting the problem so it can be reproduced ? |
I already posted this, just add the key and turn the device on the side. FlutterMap(
key: ValueKey(MediaQuery.of(context).orientation),
mapController: mapController,
) |
Does switching "late final MapState _state;" for "late MapState _state;" in map.dart fix it ? |
No, that throws:
Also it is |
Doesn't on mine, however, I suspect you are trying to do something like a mapcontroller move to fluttermap which you have removed from the widget tree, by changing it's key. That's a slightly different problem (and hence why proper examples are useful). |
I managed to resolve the issue for my case. I was using the same Using new |
I believe the problem lies in the controller being initialized outside of MapOptions. the correct way to initialize the controller should be:
|
@johnpryan I have the same issue after upgrading to import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final MapController _mapController = MapController();
bool showMap = true;
void _toggleShowMap() {
setState(() {
showMap = !showMap;
});
}
@override
Widget build(BuildContext context) {
final mapWidget = FlutterMap(
mapController: _mapController,
options: MapOptions(
zoom: 13.0,
),
layers: [
TileLayerOptions(
urlTemplate: "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
subdomains: ['a', 'b', 'c']),
],
);
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: showMap ? mapWidget : Text('List view here!'),
floatingActionButton: FloatingActionButton(
onPressed: _toggleShowMap,
tooltip: 'Toggle map',
child: Icon(showMap ? Icons.toggle_off : Icons.toggle_on),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
|
I do think the whole mapController thing could do with a bit of reworking in flutter_map (I sometimes wonder if it's even needed), but in the meantime, I've got around this by using Offstage widgets. Put flutter_map inside that, and just mark it offstage depending on 'showMap". That will keep flutter_map in the widget tree and preserve state (hence avoiding that error). |
Is there any updates? |
The mapController is certainly relevant. I've used it in conjunction with the geolocator package to show the current user position: mapController.move(LatLng(lat, long), currentZoom); However, I'm getting the same error as @SebastianEngel when calling the @ibrierley your proposed fix for offstaging the FlutterMap widget does not quite work if you have dynamic |
If you have dynamic layers, why mark them as final ? (I suspect I'm misunderstanding something here, which is why code is always good). |
I get this error when I call If you are passing in a I am using version |
Have you taken flutter_map out of the widget tree (i.e navigated away from flutter_map) but not the mapController ? Have you tried having them both in the same widget that gets removed together ? |
Yes, it happens even when the class MapView extends StatefulWidget {
final LatLng? center;
const MapView({
this.center,
Key? key,
}) : super(key: key);
@override
_MapViewState createState() => _MapViewState();
}
class _MapViewState extends State<MapView> {
final MapController _mapController = MapController();
@override
void didUpdateWidget(MapView oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.center != null && widget.center != oldWidget.center) {
_mapController.move(widget.center!, 9);
}
}
@override
Widget build(BuildContext context) {
return FlutterMap(
options: MapOptions(
bounds: _getBounds(),
controller: _mapController,
),
children: [
TileLayerWidget(
options: TileLayerOptions(
urlTemplate: 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
subdomains: ['a', 'b', 'c'],
),
),
],
);
}
} I tried moving the MapController out to the parent and just directly calling
|
I think your mapController should be a param to FlutterMap, not to MapOptions ?
I'd also be tempted to init mapController in an initState method. |
Yes, it seems like my only issue was setting the map controller in the options instead of the FlutterMap. Is there any reason to have a controller field in the options? Is there any time when you would want to set that? |
In the absence of any other replies, I have no idea, and I find the docs insufficient on this and the whole mapController thing, these seem to be common trip up points, and it's difficult often to give proper help without knowing the correct intention and design for these things (I'd add to the docs if I knew myself!). |
Marking as high priority because this is an especially bad bug. |
thanks, I solved this problem by using this way. |
it seems anybody does not develop this package. |
no, the 0.13.1 does not work too. |
Hi @alirezat66, |
@alirezat66 Could you provide more information to help us debug and fix this issue? Would be greatly appreciated :) |
sory, I just though you put out this issue. no actually I check it carefully and it has problem with controller. the problem accured when you use it in state full widget more than once for example you use map in a screen and again back to that screen so it seems the controller was not disposed |
I use get_it for solving problem and create a singleton map controller object but the problem didn't solve |
the problem solved by this : Widget build(BuildContext context) { but you have to avoid defining controller directly for FlutterMap |
You can define and use mapController, you just need to be careful not to remove one of them from the widget tree and leave the other I think (so may need wrapping in an outer widget, or wrap flutter_map in an Offstage widget or similar). |
Remove the mapController param was the solution for me, thank :-) |
Related to #986? |
@twogood Yeah, I'm pretty sure they were talking about similar things |
Hi Jaff, thanks again for your fantastic package. It helps me in many projects. |
@alirezat66 thank you very much, it means a lot. But if your talking about this package, I've only recently become a maintainer! If your talking about my package, extra thanks :) |
This worked for me.
|
I also have the same error as this. |
Try passing the mapController as a parameter to FlutterMap widget. You're also probably better off initialising the mapController in an init() method as well. There is a mapController example in the examples/pages directory that's worth following first before changing. |
One of the solution is to put the map in other class in other to have is
state indépendant of the parent who gonna call him
…On Sat, 23 Apr 2022, 08:19 Ian, ***@***.***> wrote:
Try passing the mapController as a parameter to FlutterMap widget. You're
also probably better off initialising the mapController in an init() method
as well. There is a mapController example in the examples/pages directory
that's worth following first before changing.
—
Reply to this email directly, view it on GitHub
<#926 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AJSWGQ7J5V6GGWEL2D7YGR3VGOI5RANCNFSM46JL6QUA>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days. |
I'm going to close this now, as there are workarounds available, and without a big refactoring, many of these issues will be hidden. If anyone wants to keep this open, just ping me! |
Another (hackish) way to achieve the old behavior is to copy late final MapState _state;
@override
StreamSink<MapEvent> get mapEventSink => _mapEventSink.sink; to late MapState _state;
@override
StreamSink<MapEvent> get mapEventSink {
final stream = StreamController<MapEvent>.broadcast();
final sub = stream.stream.listen((event) => _mapEventSink.sink.add(event));
stream.sink.done.then((value) => sub.cancel());
return stream.sink;
} |
|
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map/src/map/flutter_map_state.dart';
/// Used to get MapState
final _mapKey = GlobalKey<FlutterMapState>();
/// Used to get mapController
MapController? get mapController => _mapKey.currentState?.mapController;
FlutterMap(
key: _mapKey,
mapController: MapControllerImpl(),
options: MapOptions(
......
),
) |
I just upgraded to version 0.13.1 which solved already some LateInitializationErrors.
Unfortunately I just found another one in map.dart.
late final MapState _state;
which is final but can be set to another value again:
The text was updated successfully, but these errors were encountered: