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

The following _CastError was thrown during paint(): Null check operator used on a null value #124

Closed
SeCo89 opened this issue Feb 24, 2023 · 4 comments

Comments

@SeCo89
Copy link

SeCo89 commented Feb 24, 2023

Hi there,

I encounter this problem using this plugin:

======== Exception caught by rendering library =====================================================
The following _CastError was thrown during paint():
Null check operator used on a null value

The relevant error-causing widget was: 
  VectorTileLayer VectorTileLayer:file:///Users/xxxx/lib/UI/screens/map_page.dart:204:13
When the exception was thrown, this was the stack: 
#0      TextApproximation.renderer (package:vector_tile_renderer/src/features/text_renderer.dart:63:76)
#1      SymbolPointRenderer._preciselyOccupyLabelSpaceAtOffset (package:vector_tile_renderer/src/features/symbol_point_renderer.dart:89:22)
#2      SymbolPointRenderer._occupyLabelSpaceAtOffset (package:vector_tile_renderer/src/features/symbol_point_renderer.dart:80:12)
#3      SymbolPointRenderer.render (package:vector_tile_renderer/src/features/symbol_point_renderer.dart:56:12)
#4      FeatureDispatcher.render (package:vector_tile_renderer/src/features/feature_renderer.dart:49:16)
#5      DefaultLayer.render.<anonymous closure> (package:vector_tile_renderer/src/themes/theme_layers.dart:49:35)
#6      TileSpaceMapper.drawInTileSpace (package:vector_tile_renderer/src/features/tile_space_mapper.dart:39:7)
#7      DefaultLayer.render (package:vector_tile_renderer/src/themes/theme_layers.dart:47:31)
#8      Renderer.render.<anonymous closure> (package:vector_tile_renderer/src/renderer.dart:61:20)
#9      Timeline.timeSync (dart:developer/timeline.dart:160:22)
#10     profileSync (package:vector_tile_renderer/src/profiling.dart:16:19)
#11     Renderer.render (package:vector_tile_renderer/src/renderer.dart:38:5)
#12     VectorTilePainter.paint (package:vector_map_tiles/src/grid/tile/tile_painter.dart:50:10)
#13     _DelayCustomPainter.paint (package:vector_map_tiles/src/grid/tile/delay_painter.dart:164:17)
#14     RenderCustomPaint._paintWithPainter (package:flutter/src/rendering/custom_paint.dart:571:13)
#15     RenderCustomPaint.paint (package:flutter/src/rendering/custom_paint.dart:613:7)
#16     RenderObject._paintWithContext (package:flutter/src/rendering/object.dart:2796:7)
#17     PaintingContext.paintChild (package:flutter/src/rendering/object.dart:239:13)
#18     RenderProxyBoxMixin.paint (package:flutter/src/rendering/proxy_box.dart:144:15)
#19     RenderObject._paintWithContext (package:flutter/src/rendering/object.dart:2796:7)
#20     PaintingContext._repaintCompositedChild (package:flutter/src/rendering/object.dart:155:11)
#21     PaintingContext.repaintCompositedChild (package:flutter/src/rendering/object.dart:98:5)
#22     PaintingContext._compositeChild (package:flutter/src/rendering/object.dart:250:7)
#23     PaintingContext.paintChild (package:flutter/src/rendering/object.dart:231:7)
#24     RenderProxyBoxMixin.paint (package:flutter/src/rendering/proxy_box.dart:144:15)
#25     RenderAnimatedOpacityMixin.paint (package:flutter/src/rendering/proxy_box.dart:1080:11)
#26     RenderObject._paintWithContext (package:flutter/src/rendering/object.dart:2796:7)
#27     PaintingContext._repaintCompositedChild (package:flutter/src/rendering/object.dart:155:11)
#28     PaintingContext.repaintCompositedChild (package:flutter/src/rendering/object.dart:98:5)
#29     PaintingContext._compositeChild (package:flutter/src/rendering/object.dart:250:7)
#30     PaintingContext.paintChild (package:flutter/src/rendering/object.dart:231:7)
#31     RenderBoxContainerDefaultsMixin.defaultPaint (package:flutter/src/rendering/box.dart:2900:15)
#32     RenderStack.paintStack (package:flutter/src/rendering/stack.dart:654:5)
#33     RenderStack.paint (package:flutter/src/rendering/stack.dart:670:7)
#34     RenderObject._paintWithContext (package:flutter/src/rendering/object.dart:2796:7)
#35     PaintingContext.paintChild (package:flutter/src/rendering/object.dart:239:13)
#36     RenderBoxContainerDefaultsMixin.defaultPaint (package:flutter/src/rendering/box.dart:2900:15)
#37     RenderStack.paintStack (package:flutter/src/rendering/stack.dart:654:5)
#38     PaintingContext.pushLayer (package:flutter/src/rendering/object.dart:460:12)
#39     PaintingContext.pushClipRect (package:flutter/src/rendering/object.dart:520:7)
#40     RenderStack.paint (package:flutter/src/rendering/stack.dart:660:38)
#41     RenderObject._paintWithContext (package:flutter/src/rendering/object.dart:2796:7)
#42     PaintingContext.paintChild (package:flutter/src/rendering/object.dart:239:13)
#43     RenderBoxContainerDefaultsMixin.defaultPaint (package:flutter/src/rendering/box.dart:2900:15)
#44     RenderStack.paintStack (package:flutter/src/rendering/stack.dart:654:5)
#45     RenderStack.paint (package:flutter/src/rendering/stack.dart:670:7)
#46     RenderObject._paintWithContext (package:flutter/src/rendering/object.dart:2796:7)
#47     PaintingContext.paintChild (package:flutter/src/rendering/object.dart:239:13)
#48     RenderBoxContainerDefaultsMixin.defaultPaint (package:flutter/src/rendering/box.dart:2900:15)
#49     RenderStack.paintStack (package:flutter/src/rendering/stack.dart:654:5)
#50     RenderStack.paint (package:flutter/src/rendering/stack.dart:670:7)
#51     RenderObject._paintWithContext (package:flutter/src/rendering/object.dart:2796:7)
#52     PaintingContext.paintChild (package:flutter/src/rendering/object.dart:239:13)
#53     RenderProxyBoxMixin.paint (package:flutter/src/rendering/proxy_box.dart:144:15)
#54     RenderTransform.paint (package:flutter/src/rendering/proxy_box.dart:2617:17)
#55     RenderObject._paintWithContext (package:flutter/src/rendering/object.dart:2796:7)
#56     PaintingContext.paintChild (package:flutter/src/rendering/object.dart:239:13)
#57     RenderShiftedBox.paint (package:flutter/src/rendering/shifted_box.dart:84:15)
#58     RenderObject._paintWithContext (package:flutter/src/rendering/object.dart:2796:7)
#59     PaintingContext.paintChild (package:flutter/src/rendering/object.dart:239:13)
#60     RenderBoxContainerDefaultsMixin.defaultPaint (package:flutter/src/rendering/box.dart:2900:15)
#61     RenderStack.paintStack (package:flutter/src/rendering/stack.dart:654:5)
#62     RenderStack.paint (package:flutter/src/rendering/stack.dart:670:7)
#63     RenderObject._paintWithContext (package:flutter/src/rendering/object.dart:2796:7)
#64     PaintingContext.paintChild (package:flutter/src/rendering/object.dart:239:13)
#65     RenderProxyBoxMixin.paint (package:flutter/src/rendering/proxy_box.dart:144:15)
#66     PaintingContext.pushLayer (package:flutter/src/rendering/object.dart:460:12)
#67     PaintingContext.pushClipRect (package:flutter/src/rendering/object.dart:520:7)
#68     RenderClipRect.paint (package:flutter/src/rendering/proxy_box.dart:1554:25)
#69     RenderObject._paintWithContext (package:flutter/src/rendering/object.dart:2796:7)
#70     PaintingContext.paintChild (package:flutter/src/rendering/object.dart:239:13)
#71     RenderProxyBoxMixin.paint (package:flutter/src/rendering/proxy_box.dart:144:15)
#72     RenderObject._paintWithContext (package:flutter/src/rendering/object.dart:2796:7)
#73     PaintingContext.paintChild (package:flutter/src/rendering/object.dart:239:13)
#74     RenderProxyBoxMixin.paint (package:flutter/src/rendering/proxy_box.dart:144:15)
#75     RenderObject._paintWithContext (package:flutter/src/rendering/object.dart:2796:7)
#76     PaintingContext.paintChild (package:flutter/src/rendering/object.dart:239:13)
#77     RenderProxyBoxMixin.paint (package:flutter/src/rendering/proxy_box.dart:144:15)
#78     RenderObject._paintWithContext (package:flutter/src/rendering/object.dart:2796:7)
#79     PaintingContext.paintChild (package:flutter/src/rendering/object.dart:239:13)
#80     _RenderLayoutBuilder.paint (package:flutter/src/widgets/layout_builder.dart:341:15)
#81     RenderObject._paintWithContext (package:flutter/src/rendering/object.dart:2796:7)
#82     PaintingContext.paintChild (package:flutter/src/rendering/object.dart:239:13)
#83     RenderBoxContainerDefaultsMixin.defaultPaint (package:flutter/src/rendering/box.dart:2900:15)
#84     RenderStack.paintStack (package:flutter/src/rendering/stack.dart:654:5)
#85     RenderStack.paint (package:flutter/src/rendering/stack.dart:670:7)
#86     RenderObject._paintWithContext (package:flutter/src/rendering/object.dart:2796:7)
#87     PaintingContext.paintChild (package:flutter/src/rendering/object.dart:239:13)
#88     RenderProxyBoxMixin.paint (package:flutter/src/rendering/proxy_box.dart:144:15)
#89     RenderObject._paintWithContext (package:flutter/src/rendering/object.dart:2796:7)
#90     PaintingContext._repaintCompositedChild (package:flutter/src/rendering/object.dart:155:11)
#91     PaintingContext.repaintCompositedChild (package:flutter/src/rendering/object.dart:98:5)
#92     PipelineOwner.flushPaint (package:flutter/src/rendering/object.dart:1116:31)
#93     RendererBinding.drawFrame (package:flutter/src/rendering/binding.dart:515:19)
#94     WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:884:13)
#95     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:378:5)
#96     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1175:15)
#97     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1104:9)
#98     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:1015:5)
#99     _invoke (dart:ui/hooks.dart:148:13)
#100    PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:318:5)
#101    _drawFrame (dart:ui/hooks.dart:115:31)
The following RenderObject was being processed when the exception was fired: RenderCustomPaint#7e45c
...  parentData: <none> (can use size)
...  constraints: BoxConstraints(w=256.5, h=256.5)
...  size: Size(256.5, 256.5)
...  painter: _DelayCustomPainter#6be28()
RenderObject: RenderCustomPaint#7e45c
  parentData: <none> (can use size)
  constraints: BoxConstraints(w=256.5, h=256.5)
  size: Size(256.5, 256.5)
  painter: _DelayCustomPainter#6be28()
==================================================================================================== 

It disappears after one second, but sometimes when I reload the app, it appears again.
I know that the error occurs when you use a ! operator on a nullable instance which wasn't initialized. But initializing styledSymbol doesn't help.

Are there any suggestions?

@MendleM
Copy link

MendleM commented Feb 25, 2023

Can you share your code? I'm guessing that the style is asynchronously fetched and so it hasn't yet initialized when the map is drawn.

@SeCo89
Copy link
Author

SeCo89 commented Mar 1, 2023

Thanks for the quick response. My code is very similar to the example given. Here are the relevant methods:

@override
  void initState() {
    keepAlive = widget.isSelectLocationMode ? false : true;
    _mapController = MapController();
    _popupController = PopupController();
    super.initState();
    userLocationMarkers = [];
    markers = [];
    parkingSpaces = [];
    mapZoom = widget.isSelectLocationMode
        ? AppConstants.MAX_MAP_ZOOM
            : AppConstants.MIN_MAP_ZOOM;

    _initStyle();
  }

@override
  Widget build(BuildContext context) {
    super.build(context);
    return Stack(
      children: [
        _error != null
            ? Expanded(child: Text(_error.toString()))
            : _style == null
                ? const Center(child: CircularProgressIndicator())
                : _map(),
        Helpers.getLocationProvider(context, false).hasPermission ? _locateFloatingButton() : SizedBox(),
        isLocating ? ProcessIndicatorWidget() : SizedBox(),
      ],
    );
  }

void _initStyle() async {
    try {
      _style = await _readStyle();
    } catch (e, stack) {
      // ignore: avoid_print
      print(e);
      // ignore: avoid_print
      print(stack);
      _error = e;
    }
    setState(() {});
  }

Future<Style> _readStyle() => StyleReader(
          uri: 'https://api.maptiler.com/maps/streets-v2/style.json?key={key}',
          // ignore: undefined_identifier
          apiKey: AppConstants.MAPTILER_API_KEY,
          logger: const Logger.console())
      .read();

Widget _map() => Consumer(builder: (BuildContext context, BackendService service, child) {
        return FlutterMap(
          mapController: _mapController,
          options: MapOptions(
            onMapReady: () async {
              LocationService locationService = Helpers.getLocationProvider(context, false);
              if (!widget.isSelectLocationMode) {
                locationService.isLocationRequested = true;
                positionStreamSubscription =
                await locationService.streamCurrentPosition(context, _onLocationRequested, _onLocationUpdated);
              } else {
                isLocating = false;
                if (service.addParkingSpaceKey.currentState.parkingSpace.latitude != null &&
                    service.addParkingSpaceKey.currentState.parkingSpace.longitude != null) {
                  service.updateLocationOnMap(
                    LatLng(
                      service.addParkingSpaceKey.currentState.parkingSpace.latitude,
                      service.addParkingSpaceKey.currentState.parkingSpace.longitude,
                    ),
                    true,
                  );
                } else {
                  service.updateLocationOnMap(
                    LatLng(
                      locationService.currentPosition.latitude,
                      locationService.currentPosition.longitude,
                    ),
                    true,
                  );
                }
                setParkingSpaces([
                  ParkingSpaces(
                      latitude: service.centerLocation.latitude,
                      longitude: service.centerLocation.longitude)
                ]);
                service.addParkingSpaceKey.currentState.updateView();
              }

              print('map is ready');
            },
            minZoom: AppConstants.MIN_MAP_ZOOM,
            maxZoom: AppConstants.MAX_MAP_ZOOM,
            interactiveFlags: InteractiveFlag.all & ~InteractiveFlag.rotate,
            onTap: (event, latLong) => hidePopup(),
            onPositionChanged: (mapPosition, boolValue) async {
              widget.onMapMoved(mapPosition.center);
              if (widget.isSelectLocationMode && this.markers.length > 0) {
                this.markers[0].point.latitude = mapPosition.center.latitude;
                this.markers[0].point.longitude = mapPosition.center.longitude;
              }
            },
          ),
          children: [
            VectorTileLayer(
                theme: _style.theme,
                backgroundTheme: _style.theme.copyWith(types: {ThemeLayerType.background, ThemeLayerType.fill}),
                // tileOffset: TileOffset.mapbox, enable with mapbox
                tileProviders: _style.providers),
            CurrentLocationLayer(),
            MarkerLayer(markers: userLocationMarkers),
          ],
        );
      });

     Widget _locateFloatingButton() {
    return Align(
      alignment: Alignment.bottomRight,
      child: Consumer<BackendService>(
        builder: (context, service, child) {
          return Padding(
            padding: EdgeInsets.only(
                bottom: (!(service.parkingStatus == ParkingStatus.checkingIn) && !(service.parkingStatus == ParkingStatus.checkingOut)) &&
                        !widget.isSelectLocationMode
                    ? 100.0
                    : 16.0,
                right: 16.0),
            child: (!(service.parkingStatus == ParkingStatus.checkingIn) &&
                    !(service.parkingStatus == ParkingStatus.checkingOut) &&
                    !widget.isSelectLocationMode)
                ? _locationButton()
                : SizedBox(),
          );
        },
      ),
    );
  }

The error occurs whenever the zoom value is very high. Once the map or I think style has loaded, the error disappears. if I jump to another location, the error shows again.

@SeCo89
Copy link
Author

SeCo89 commented Mar 7, 2023

Do you have any suggestions??

@greensopinion
Copy link
Owner

this is fixed in version 3.1.2 of https://pub.dev/packages/vector_tile_renderer

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

3 participants