首页 > 解决方案 > Flutter - 处理“serviceExtension”自定义请求时出错:方法不可用:ext.flutter.inspector.setPubRootDirectories

问题描述

我试图在打开屏幕时获取当前的 GPS 位置,并将地图置于当前位置的中心。

如果我不使用这个显示 MapScreen,一切正常。

该应用程序返回这个 MapScreen,下面的代码就是它所拥有的一切:

import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:location/location.dart';

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

  @override
  _MapScreenState createState() => _MapScreenState();
}

class _MapScreenState extends State<MapScreen> {
  // Maps
  late GoogleMapController mapController;

  final LatLng _center = const LatLng(45.521563, -122.677433);

  void _onMapCreated(GoogleMapController controller) {
    mapController = controller;
  }

  // Location GPS
  Location location = Location();

  late bool _serviceEnabled;
  late PermissionStatus _permissionGranted;
  late LocationData _locationData;

  Future<LatLng> getLoc() async {
    _serviceEnabled = await location.serviceEnabled();
    if (!_serviceEnabled) {
      _serviceEnabled = await location.requestService();
      if (!_serviceEnabled) {
        return _center;
      }
    }

    _permissionGranted = await location.hasPermission();
    if (_permissionGranted == PermissionStatus.denied) {
      _permissionGranted = await location.requestPermission();
      if (_permissionGranted != PermissionStatus.granted) {
        return _center;
      }
    }

    _locationData = await location.getLocation();

    return LatLng(_locationData.latitude ?? _center.latitude,
        _locationData.longitude ?? _center.latitude);
  }

  @override
  void initState() {
    super.initState();
    getLoc();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: FutureBuilder<LatLng>(
      future: getLoc(),
      builder: (BuildContext context, AsyncSnapshot<LatLng> snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          print('itemNo in FutureBuilder: ${snapshot.data}');
          return Text('Done');
        } else {
          return const Text('Loading...');
        }
      },
    ));
  }
}

当我尝试在我的 iOS 模拟器上运行该应用程序时,构建失败并出现以下错误:

Error handling 'serviceExtension' custom request: method not available: ext.flutter.inspector.setPubRootDirectories

标签: fluttergoogle-mapsgoogle-maps-api-3locationflutter-futurebuilder

解决方案


推荐阅读