Flutter Google Maps Failed assertion: line 24 pos 16: 'target != null': is not true

时间:2021-06-21 15:17:18

标签: flutter dart google-maps-api-3

我正在尝试使用 Google Maps API 获取用户的当前位置,这是我遇到的以下错误:

======== Exception caught by widgets library =======================================================
The following assertion was thrown building BlocBuilder<MapCubit, MapStates>(dirty, state: _BlocBuilderBaseState<MapCubit, MapStates>#8c0c9):
'package:google_maps_flutter_platform_interface/src/types/camera.dart': Failed assertion: line 24 pos 16: 'target != null': is not true.

这是下面包含地图的屏幕:

import 'package:airforce/modules/map/cubit/map_cubit.dart';
import 'package:airforce/modules/map/cubit/map_states.dart';
import 'package:airforce/shared/cubit/app_cubit.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

class MapScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => MapCubit()..getUserLocation(),
      child: BlocConsumer<MapCubit, MapStates>(
        listener: (context, state) {},
        builder: (context, state) {
          return GoogleMap(
            initialCameraPosition: CameraPosition(
              target: MapCubit.initialPosition,
            ),
            myLocationEnabled: true,
          );
        },
      ),
    );
  }
}

这是我拥有的下面的cubit,它具有获取当前用户位置的方法:

import 'package:airforce/modules/map/cubit/map_states.dart';
import 'package:bloc/bloc.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:geocoding/geocoding.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

class MapCubit extends Cubit<MapStates> {
  MapCubit() : super(MapInitialState());

  static MapCubit get(context) => BlocProvider.of(context);


  static LatLng initialPosition;
  static LatLng lastMapPosition = initialPosition;

  void getUserLocation() async {
    Position position = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high);
    List<Placemark> placemarks = await placemarkFromCoordinates(
        position.latitude, position.longitude);
    initialPosition = LatLng(position.latitude, position.longitude);
    emit(GetUserCurrentLocation());
    print('${placemarks[0].name}');
  }
}

这是下面的 MpState:

abstract class MapStates {}

class MapInitialState extends MapStates {}

class GetUserCurrentLocation extends MapStates {}

0 个答案:

没有答案