Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
369 views
in Technique[技术] by (71.8m points)

flutter - Geolocation isSuccessful getter showing error

I am trying to create a project on flutter using Geolocation plugin. But 'result' variable of type Geolocation is not working fine. I am using getter 'isSuccessfull' of result type. It is throwing error displaying that no such getter exist.How can I get rid of this problem.Please help.

    import 'package:flutter/material.dart';
    import 'package:flutter_map/flutter_map.dart';
    import 'package:latlong/latlong.dart';
    import 'package:geolocation/geolocation.dart';

    void main() {
      runApp(new MyCurrentLocationMap());
    }

    class MyCurrentLocationMap extends StatefulWidget {
      @override
      _MyCurrentLocationMapState createState() => _MyCurrentLocationMapState();
    }

    class _MyCurrentLocationMapState extends State<MyCurrentLocationMap> {
      MapController mapController = new MapController();

      getPermission() async {
        final GeolocationResult result =
            await Geolocation.requestLocationPermission(
                permission: const LocationPermission(
                    android: LocationPermissionAndroid.fine,
                    ios: LocationPermissionIOS.always));
        return result;

      }

      getLocation() {
        return getPermission().then((result) {
          if (result.isSuccessful) {
            final coords =
                Geolocation.currentLocation(accuracy: LocationAccuracy.best);
            print(coords);
            return coords;
          }
        });
      }

      dynamic lat, long;

      buildMap() {
        getLocation().then((response) {
          if (response.isSuccessful) {
            response.listen((value) {
              mapController.move(
                  new LatLng(value.location.latitude, value.location.longitude),
                  15.0);

              lat = value.location.latitude;
              long = value.location.longitude;
            });
          }
        });
      }

      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          home: new Scaffold(
            body: new FlutterMap(
              mapController: mapController,
              options: MapOptions(
                minZoom: 100.0,
                center: buildMap(),
              ),
              layers: [
                new TileLayerOptions(
                    urlTemplate:
                        "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
                    subdomains: ['a', 'b', 'c']),
                 new MarkerLayerOptions(markers: [
                   new Marker(
                       width: 45.0,
                       height: 45.0,
                       point: new LatLng(lat, long),
                       builder: (context) => new Container(
                             child: IconButton(
                               icon: Icon(Icons.location_on),
                               color: Colors.blue,
                               iconSize: 45.0,
                               onPressed: () {
                                 print("Market tapped");
                               },
                             ),
                           ))
                 ])
              ],
            ),
          ),
        );
      }
    }

pubspec.yaml

    name: helloWorld
    description: A new Flutter project.

    publish_to: 'none' 

    CFBundleVersion.
    # Read more about iOS versioning at
    # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
    version: 1.0.0+1

    environment:
      sdk: ">=2.7.0 <3.0.0"

    dependencies:
      flutter:
        sdk: flutter
      flutter_circular_chart:
      flutter_map:
      geolocation: ^1.0.0

     
      cupertino_icons: ^0.1.3
      http: ^0.12.0
      

    dev_dependencies:
      flutter_test:
        sdk: flutter

    
    flutter:

     


      fonts:
      
        - family: bebas-neue
          fonts:
            - asset: assets/BebasNeue-Regular.ttf

The following link shows the error image https://pasteboard.co/JLg7jra.png

question from:https://stackoverflow.com/questions/65882947/geolocation-issuccessful-getter-showing-error

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have not defined your getPermissionmethod to return a specific datatype. You probably want it to return a GeolocationResult:

Future<GeolocationResult> getPermission() async {

Your getLocation is also not doing what you think it is doing. This is not JavaScript, so please treat it like an actual programming language. Declare your types and the compiler will help you with good error messages.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...