flutter 未为类型“MapController”定义getter“onReady”

cl25kdpy  于 2023-03-31  发布在  Flutter
关注(0)|答案(1)|浏览(179)

我正在使用flutter_map构建flutter map应用程序。
当设备准备就绪时,我想获取位置并将Map移动到该位置。
但是,我的代码有问题。我不知道我做错了什么。
我得到了错误:
未为类型“MapController”定义getter“onReady”。
下面是我的代码:

主省道

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:spot_the_bird/bloc/location_cubit.dart';
import 'package:spot_the_bird/screens/map_screen.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  
  @override
  Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: [
        BlocProvider<LocationCubit>(
          create: (context) => LocationCubit()..getLocation(),
        ),
      ],
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          appBarTheme: const AppBarTheme(backgroundColor: Color(0xff1746A2)),
          colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.deepOrange),
        ),
        initialRoute: "/",
        routes: {
          "/": (context) => MapScreen(),
        },
      ),
    );
  }
}

Map画面

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart';
import 'package:spot_the_bird/bloc/location_cubit.dart';

class MapScreen extends StatelessWidget {
  MapScreen({super.key});

  final MapController _mapController = MapController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: BlocListener<LocationCubit, LocationState>(
        listener: (prevState, currState) {
          if (currState is LocationError) {
            ScaffoldMessenger.of(context).showSnackBar(
              SnackBar(
                content: Text(currState.message),
                duration: const Duration(seconds: 3),
                backgroundColor: Theme.of(context).errorColor,
              ),
            );
          }
          if (currState is LocationLoaded) {
            _mapController.onReady.then((value) => _mapController.move(
                LatLng(currState.latitude, currState.longitude), 15));
          // This is where I got the error
          }
        },
        child: FlutterMap(
          mapController: _mapController,
          options: MapOptions(
            center: LatLng(51.509364, -0.128928),
            zoom: 15,
            minZoom: 4,
          ),
          children: [
            TileLayer(
              urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
              subdomains: const ['a', 'b', 'c'],
              retinaMode: true,
            )
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          context.read<LocationCubit>().getLocation();
        },
        child: const Icon(Icons.navigation_outlined),
      ),
    );
  }
}

通过BLOC进行状态管理

位置库比特

import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:geolocator/geolocator.dart';

part 'location_state.dart';

class LocationCubit extends Cubit<LocationState> {
  LocationCubit() : super(const LocationInitial());

  Future<void> getLocation() async {
    LocationPermission locationPermission = await Geolocator.checkPermission();

    if (locationPermission != LocationPermission.denied ||
        locationPermission != LocationPermission.deniedForever) {
      emit(const LocationLoading());

      try {
        Position position = await Geolocator.getCurrentPosition(
            desiredAccuracy: LocationAccuracy.best);

        emit(LocationLoaded(
            longitude: position.longitude, latitude: position.latitude));
      } catch (error) {
        emit(const LocationError(
            message: 'Error occured while fetching location'));
      }
    } else {
      Geolocator.requestPermission();
    }
  }
}

Location_state.dart

part of 'location_cubit.dart';

abstract class LocationState extends Equatable {
  const LocationState();

  @override
  List<Object?> get props => [];
}

class LocationInitial extends LocationState {
  const LocationInitial();
}

class LocationLoading extends LocationState {
  const LocationLoading();
}

class LocationLoaded extends LocationState {
  const LocationLoaded({
    required this.longitude,
    required this.latitude,
  });
  final double longitude;
  final double latitude;

  @override
  List<Object?> get props => [latitude, longitude];
}

class LocationError extends LocationState {
  final String message;
  const LocationError({
    required this.message,
  });
}

pubspec.yaml中的依赖项

dependencies:
  flutter:
    sdk: flutter
  flutter_map: ^3.0.0
  latlong2: ^0.8.1
  geolocator: ^9.0.2
  flutter_bloc: ^8.1.1
  equatable: ^2.0.5
  bloc: ^8.1.0
  cupertino_icons: ^1.0.2
afdcj2ne

afdcj2ne1#

2个选项:
1.您可以使用2.2.0版本,onReady在3.0中被删除(请参阅更改日志)
1.在3.0版本中尝试onMapReady回调
https://pub.dev/packages/flutter_map/changelog
https://github.com/fleaflet/flutter_map/pull/1333
https://docs.fleaflet.dev/migration/to-v3.0.0#removed-mapcontroller-.onready

相关问题