簡體   English   中英

class flutter 內訪問變量

[英]Access variable within the class flutter

我正在嘗試訪問 class _destLat 和 _destLong 中的變量

final CameraPosition _addBusiness = CameraPosition(
      target: LatLng(_destLat, _destLong), zoom: 18.151926040649414);


void initState() {
   
    late double _destLat = widget.restLatitude;
    late double _destLong = widget.restLongitude;


    super.initState();
  }

錯誤是 _destLat 和 destLong 未定義。

這是完整的 class。這個 class 返回一個 map 小部件,在 map 小部件上有 3 個按鈕控制 map 上的操作,在 map 上找到標記,返回初始頁面 17.838713

 class BarMapWidget extends StatefulWidget {

  final double restLatitude;
  final double restLongitude;


  const BarMapWidget(
      this.restLatitude,
      this.restLongitude,
      {Key? key}
      )
      : super(key: key);

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

class _BarMapWidgetState extends State<BarMapWidget> {
  final Completer<GoogleMapController> _controller = Completer();
  Map<MarkerId, Marker> markers = {};


  // Destination Longitude

  late final double _destLatitude = widget.restLatitude;
  late final double _destLongitude = widget.restLongitude;


  ///////////// Business Position /////////////

  final CameraPosition _addBusiness = CameraPosition(
      target: LatLng(_destLat, _destLong), zoom: 18.151926040649414);


  final CameraPosition _kilkennyCity = const CameraPosition(
    target: LatLng(constants.kilkenny_Latitude, constants.kilkenny_Longitude),
    zoom: 15.4746,
  );

  void initState() {
    // Add destination marker
    _addMarker(
        LatLng(_destLatitude, _destLongitude),
        "destination",
        // BitmapDescriptor.defaultMarkerWithHue(90),
        BitmapDescriptor.defaultMarker);

    late double _destLat = widget.restLatitude;
    late double _destLong = widget.restLongitude;


    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Text(_destLatitude.toString()),
        Text(_destLongitude.toString()),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            ///////////////     Rest Button ///////////////

            ElevatedButton(
              style: ElevatedButton.styleFrom(
                primary: const Color(0xFFd0d0d1),
                padding:
                    const EdgeInsets.symmetric(horizontal: 5, vertical: 10),
              ),
              onPressed: _reset,
              child: const Text(
                'City Centre ',
                style: TextStyle(
                  fontSize: 15,
                  fontWeight: FontWeight.bold,
                  color: Color(0xFF0040dd),
                ),
              ),
            ),
            ///////////////   Find on Map  ///////////////
            ElevatedButton(
              style: ElevatedButton.styleFrom(
                primary: const Color(0xFFd0d0d1),
                padding:
                    const EdgeInsets.symmetric(horizontal: 5, vertical: 10),
              ),
              onPressed: _findOnMap,
              child: const Text(
                'Find on map',
                style: TextStyle(
                  fontSize: 15,
                  fontWeight: FontWeight.bold,
                  color: Color(0xFF0040dd),
                ),
              ),
            ),

            ///////////////     All Listings  ///////////////
            ElevatedButton(
              style: ElevatedButton.styleFrom(
                primary: const Color(0xFFd0d0d1),
                padding:
                    const EdgeInsets.symmetric(horizontal: 5, vertical: 10),
              ),
              onPressed: () {
                Navigator.pop(context);
              },
              child: const Text(
                'All',
                style: TextStyle(
                  fontSize: 15,
                  fontWeight: FontWeight.bold,
                  color: Color(0xFF0040dd),
                ),
              ),
            ),
          ],
        ),
        const SizedBox(height: 10),
        SizedBox(
          height: 500,
          child: GoogleMap(
            mapType: MapType.normal,
            initialCameraPosition: _kilkennyCity,
            markers: Set<Marker>.of(markers.values),
            myLocationEnabled: false,
            myLocationButtonEnabled: false,
            tiltGesturesEnabled: true,
            compassEnabled: true,
            scrollGesturesEnabled: true,
            zoomGesturesEnabled: true,
            onMapCreated: (GoogleMapController controller) {
              _controller.complete(controller);
            },
          ),
        ),
      ],
    );
  }

  // This method will add markers to the map based on the LatLng position
  _addMarker(LatLng position, String id, BitmapDescriptor descriptor) {
    MarkerId markerId = MarkerId(id);
    Marker marker =
        Marker(markerId: markerId, icon: descriptor, position: position);
    markers[markerId] = marker;
  }

  Future<void> _findOnMap() async {
    final GoogleMapController controller = await _controller.future;
    controller.animateCamera(CameraUpdate.newCameraPosition(_addBusiness));
  }

  Future<void> _reset() async {
    final GoogleMapController controller = await _controller.future;
    controller.animateCamera(CameraUpdate.newCameraPosition(_kilkennyCity));
  }
}

問題:

變量_destLat_destLong僅在initState方法的 scope 中定義,這使得它們在 class 中未定義。

解決方案:

您可以通過在 state class 中的initState方法之外定義它們,使它們對_addBusiness可用。

由於您需要_destLat_destLong變量來定義_addBusiness變量,因此您應該將_addBusiness的賦值_destLat_destLong的賦值之后。

下面是代碼演示:

  late double _destLat;
  late double _destLong;
  late CameraPosition _addBusiness;
 
  void initState() {
    // Add destination marker
    _addMarker(
        LatLng(_destLatitude, _destLongitude),
        "destination",
        // BitmapDescriptor.defaultMarkerWithHue(90),
        BitmapDescriptor.defaultMarker);

    _destLat = widget.restLatitude;
    _destLong = widget.restLongitude;

    _addBusiness = CameraPosition(
      target: LatLng(_destLat, _destLong), zoom: 18.151926040649414);

    super.initState();
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM