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

RangeError (index): Invalid value: Valid value range is empty: 0 #1146

Closed
kamilratulowski opened this issue Feb 1, 2022 · 7 comments
Closed
Labels
bug This issue reports broken functionality or another error

Comments

@kamilratulowski
Copy link

kamilratulowski commented Feb 1, 2022

Can anyone tell me what i did wrong?
This is the problem:
The relevant error-causing widget was FlutterMap
RangeError (index): Invalid value: Valid value range is empty: 0

import 'package:flutter/material.dart';

import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart';

class DataScreen extends StatelessWidget {
  DataScreen({Key? key}) : super(key: key);
  static const routeName = '/data-screen';
  final markers = <Marker>[];

  void addMarker() {
    Marker marker = Marker(
      point: LatLng(49.23175824, 19.98163184),
      builder: (ctx) => const Icon(Icons.menu),
    );
    markers.add(marker);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          FlutterMap(
            options: MapOptions(
              center: LatLng(49.2849, 19.9732),
            ),
            layers: [
              TileLayerOptions(
                  urlTemplate:
                      'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
                  subdomains: ['a', 'b', 'b'],
                  fastReplace: true),
              MarkerLayerOptions(
                markers: markers,
              )
            ],
          ),
          Positioned(
            top: 30,
            left: 50,
            child: InkWell(
              onTap: () {
                addMarker();
              },
              child: const Text('PRESS'),
            ),
          )
        ],
      ),
    );
  }
}
@ibrierley
Copy link
Contributor

Is there a specific line it points to with the error ?

@kamilratulowski
Copy link
Author

Is there a specific line it points to with the error ?

24 FlutterMap()

@kamilratulowski
Copy link
Author

Is there a specific line it points to with the error ?

When i start scrolling on the screen everything is ok

@ibrierley
Copy link
Contributor

ibrierley commented Feb 1, 2022

Bit technical, just in case someone else reads as well and has some ideas, as I'm a bit tied up atm, but may get chance to have a better look later..

A quick rummage...(so may be off target)

I think there's a bug in FlutterMaps MarkerLayer, where it barfs if the pxCache has a different number of markers to the actual number markers. So I think this may happen a lot in cases where people add new markers...

I note we regenerate pxCache if didUpdateWidget is called or the zoom changes...maybe it needs to also be called if px cache size is different to marker list size as well....

Suggestion for fluttermap bug fix.

if(widget.markerLayerOptions.markers.length != _pxCache.length) {
          _pxCache = generatePxCache();
}

Not entirely happy with that though, as I guess its possible values may have changed but size the same....

@ibrierley
Copy link
Contributor

so, just thinking out loud on this a bit more.

Let's assume someone may have 10,000 markers, projections internally cached, not in view, and they change the last marker LatLng which makes it now come in view. What should happen ?

A length check (which I think would fix the crash and maybe ok as a very temp solution), wouldn't help here. So what are the options ?

  1. Don't have caching

  2. rescan every Marker to see if it's changed. Probably more optimal than recalculating projections every go, but I assume some performance downside.

  3. Give the user the option to use the cache or not, with clear docs explaining issues.

  4. Some dark magic to store if its added/deleted to (not sure if this is possible and make it easy to user. I think LatLngs have to be created and can't be mutated, but I may be out of touch on that)

  5. Have 2 widgets, a MarkerLayer and a CachedMarkerLayer

Not sure I like any of those :D, anyone else have any ideas (or am I missing something and the issue is wrong)?

@ibrierley
Copy link
Contributor

There's a PR at #1147 to get around the problem if you wanted a test. Digging into it a bit more, you may be able to scrape by without the crash and PR by calling a setState({}) after you add the marker (which is probably wise anyway to update the display).

@JaffaKetchup JaffaKetchup added bug This issue reports broken functionality or another error non-fatal labels Feb 8, 2022
@ibrierley
Copy link
Contributor

Going to close this, but feel free to reopen if #1147 doesn't fix the problem for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue reports broken functionality or another error
Projects
None yet
Development

No branches or pull requests

3 participants