Search code examples
flutterwidgetnull-check

Null check operator used on a null value error in widgets library


this happened after I insert google map code I'm creating a tracking app which will use the google map to display the tracked location

the error is shown as bellow

======== Exception caught by widgets library ======================================================= The following _TypeError was thrown building busNearby(dirty, state: _busNearbyState#460c7): Null check operator used on a null value

enter image description here enter image description here enter image description here enter image description here enter image description here

 Widget build(BuildContext context) {
return Scaffold(
    appBar: AppBar(
      title: const Text('TRACKER'),
      centerTitle: true,
      backgroundColor: Colors.amber[500],
    ),
    body:  Stack(
      children: <Widget>[
        Column(
          children: [
            const HoveringWidget(),
            GoogleMap(
              initialCameraPosition: const CameraPosition(
                  target: _utm,
                  zoom: 13),
              markers: {
                Marker(
                    markerId: MarkerId("_currentLocation"),
                    icon: BitmapDescriptor.defaultMarker,
                    position: _currentP!),
                Marker(
                    markerId: MarkerId("_sourceLocation"),
                    icon: BitmapDescriptor.defaultMarker,
                    position: _utm),
                Marker(
                    markerId: MarkerId("_destinationLoaction"),
                    icon: BitmapDescriptor.defaultMarker,
                    position: _paradigm)
              },

            ),

          ],
        ),
      ],
    ),

Solution

  • I can assume that _currentP is not initialized before you use it. You can either use your preferred state manager or FutureBuilder to resolve this issue.

    FutureBuilder Example:

    FutureBuilder(
        future: getCurrentP(),
        builder: ((context, snapshot) {
          if (!snapshot.hasData) {
            return const SizedBox();
          }
    
          return Marker(
              markerId: MarkerId("_currentLocation"),
              icon: BitmapDescriptor.defaultMarker,
              position: snapshot.data!);
        }));