Skip to content
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 with polygonCulling in PolygonLayerOptions - with example #1037

Closed
spatialbits opened this issue Sep 20, 2021 · 12 comments

Comments

@spatialbits
Copy link

spatialbits commented Sep 20, 2021

Same as #974 and #931 but opening as a new issue since those were closed.

Upgrading FlutterMap from very old version I am encountering LateInitializationError: Field 'boundingBox' has already been initialized when polygonCulling=true.

The problem crops up when I hold reference to polygonOpts in a stateful widget. This seems reasonable to me as rebuilding polygonOpts would be unnecessary.

Example
import 'package:flutter/material.dart';
import 'package:flutter_map/plugin_api.dart';
import 'package:latlong2/latlong.dart' hide Path; // conflict with Path from UI

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MapPage(),
    );
  }
}

class MapPage extends StatefulWidget {
  const MapPage({Key? key}) : super(key: key);

  @override
  State<MapPage> createState() => _MapPageState();
}

class _MapPageState extends State<MapPage> {
  final MapController _mapController = MapController();

  final poly = Polygon(
    points: [
      LatLng(50, -120),
      LatLng(50, -119),
      LatLng(49, -119),
      LatLng(49, -120),
    ],
  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FlutterMap(
        options: MapOptions(
          center: LatLng(49.4, -119.5),
          zoom: 8,
          onTap: (_, __) => setState(() {}),
          controller: _mapController,
        ),
        nonRotatedChildren: [
          TileLayerWidget(
            options: TileLayerOptions(
                minZoom: 2,
                maxZoom: 19,
                urlTemplate:
                    "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
                subdomains: ['a', 'b', 'c']),
          ),
          PolygonLayerWidget(
            options: PolygonLayerOptions(
              polygonCulling: true,
              polygons: [poly],
              // polygons: [
              //   Polygon(
              //     points: [
              //       LatLng(50, -120),
              //       LatLng(50, -119),
              //       LatLng(49, -119),
              //       LatLng(49, -120),
              //     ],
              //   )
              // ],
            ),
          )
        ],
      ),
    );
  }
}
  • Tap in the map to trigger setState and exception will be thrown.
  • Swap polygons: [poly], for the inline Polygon instance and exceptions won't be thrown.
@moovida
Copy link
Contributor

moovida commented Sep 24, 2021

Same issue here.

class PolylineLayerOptions extends LayerOptions {
[...]
    if (polylineCulling) {
      for (var polyline in polylines) {
        polyline.boundingBox = LatLngBounds.fromPoints(polyline.points);
[...]

Can't it be assumed that if the bbox is already calculated, it probably didn't change?

@ibrierley
Copy link
Collaborator

Any difference if poly or polylines isn't final ?

@moovida
Copy link
Contributor

moovida commented Sep 24, 2021

Not sure I understand your comment.
Since the bbox is final:

late final LatLngBounds boundingBox;

There is no way to reset it.

@moovida
Copy link
Contributor

moovida commented Sep 24, 2021

The interesting thing is that there isn't even a way to check if it is initialized or not? Can that be?
If I try to check:

if (polyLine.boundingBox != null) {

It throws me an exception: LateInitializationError: Field 'boundingBox' has not been initialized.

@spatialbits
Copy link
Author

spatialbits commented Sep 24, 2021

Any difference if poly or polylines isn't final ?

Yeah it does make a difference.

This is my fix:
Kwusen@f07fd48

Also, since my bboxes are calculated elsewhere I parameterized it to avoid the extra overhead.

EDIT:
Sorry, it does make a difference if boundingBox isn't final. I haven't checked if making polygons or polylines non-final has any effect; I don't expect it would.

@mohammedX6
Copy link

mohammedX6 commented Sep 29, 2021

This is working for me without using late modifier
try to set boundingBox to this in the Polygon Class
LatLngBounds boundingBox = LatLngBounds();

@github-actions
Copy link

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.

@github-actions github-actions bot added the Stale label Oct 30, 2021
@github-actions
Copy link

github-actions bot commented Nov 4, 2021

This issue was closed because it has been stalled for 5 days with no activity.

@github-actions github-actions bot closed this as completed Nov 4, 2021
@Vafin84
Copy link

Vafin84 commented Dec 23, 2021

I want to inform you that the problem is still relevant with polylines

jithware added a commit to jithware/flutter_map that referenced this issue Dec 24, 2021
@jithware
Copy link
Contributor

Here's an example that reproduces the error and a proposed fix: jithware@dd418f8

@beeekey
Copy link

beeekey commented Feb 21, 2023

This Bug still exists?

@ibrierley
Copy link
Collaborator

What exact error and what exact version of flutter_map are you using ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants