geolocator 6.0.0 + 1 distanceBetween()在颤动中不起作用 [英] geolocator 6.0.0+1 distanceBetween() not working in flutter

查看:100
本文介绍了geolocator 6.0.0 + 1 distanceBetween()在颤动中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望每个人都安全,我正在使用颤振,并且我想计算两个位置之间的距离,所以我已经从pub.dev 此处

i hope everyone is safe i am using flutter,and i want to calculate distance between two locations so i have installed geolocator 6.0.0+1 from pub.dev here, with that i can find the location of user but i cannot use the inbuilt method called ** distanceBetween()** which is used to calculate the distance between two latitudes and longitudes, it shows me a error whis says **The method 'distanceBetween' isn't defined for the type '_RegisterState'. ** i have used the method as shown in the documentation here

这是我的代码:

import 'dart:html;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:staffattendence05/Usermodel.dart';
import 'package:staffattendence05/add_user.dart';
import 'package:staffattendence05/authenticate.dart';
import 'package:staffattendence05/fauth.dart';
import 'package:staffattendence05/home.dart';
import 'package:staffattendence05/styling.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:staffattendence05/loading.dart';
import 'package:geolocator/geolocator.dart';
import 'Login_ins.dart';

class Register extends StatefulWidget {
  @override
  _RegisterState createState() => _RegisterState();
}

class _RegisterState extends State<Register> {
  @override
  final _formkey = GlobalKey<FormState>();

  String email = '';

  String error = '';

  String password = '';

  bool loading = false;

  final Authservice _auth = Authservice();

  Widget build(BuildContext context) {
    return loading
        ? Loading()
        : Scaffold(
            backgroundColor: Colors.teal,
            appBar: AppBar(
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(5),
              ),
              title: Text(
                "Register your institute",
                style:
                    TextStyle(fontWeight: FontWeight.bold, color: Colors.white),
              ),
              actions: <Widget>[
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: FlatButton.icon(
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(10)),
                    onPressed: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(builder: (context) => Loginins()),
                      );
                    },
                    icon: Icon(Icons.perm_identity),
                    label: Text("institute Login"),
                    color: Colors.teal,
                  ),
                )
              ],
              backgroundColor: Colors.green[400],
            ),
            body: Padding(
              padding: const EdgeInsets.all(36.0),
              child: Form(
                key: _formkey,
                child: Column(
                  children: <Widget>[
                    SizedBox(
                      height: 120.0,
                    ),
                    TextFormField(
                      decoration: styling.copyWith(hintText: "Admin email"),
                      validator: (val) {
                        return val.isEmpty
                            ? "please enter an valid email id"
                            : null;
                      },
                      onChanged: (val) {
                        email = val;
                      },
                    ),
                    SizedBox(
                      height: 20.0,
                    ),
                    TextFormField(
                      obscureText: true,
                      decoration: styling.copyWith(hintText: "Admin password"),
                      validator: (val) => val.length < 6
                          ? "a password should contain atleast 6 characters"
                          : null,
                      onChanged: (val) => password = val,
                    ),
                    SizedBox(
                      height: 25.0,
                    ),
                    RaisedButton(
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(7)),
                      color: Colors.green[400],
                      child: Text(
                        "Register",
                        style: TextStyle(fontWeight: FontWeight.bold),
                      ),
                      onPressed: () async {
                        if (_formkey.currentState.validate()) {
                          setState(() {
                            loading = true;
                          });
                          Position Insposition = await getCurrentPosition(
                              desiredAccuracy: LocationAccuracy.best);
                          dynamic result =
                              await _auth.Signupwithemailandpassword(
                                  email, password, Insposition);
                          final String adminname = email;
                          double distanceInMeters = distanceBetween(
                              52.2165157, 6.9437819, 52.3546274, 4.8285838);
                          print("1111111111111$adminname");
                          print(Insposition);
                          if (result == null) {
                            setState(() {
                              loading = false;
                              error = 'Please enter a valid email address!';
                            });
                          } else {
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => Home(
                                        adminname: adminname,
                                      )),
                            );
                          }
                        }
                      },
                    ),
                    SizedBox(height: 20.0),
                    Text(
                      error,
                      style: TextStyle(
                          color: Colors.red[400],
                          fontWeight: FontWeight.bold,
                          fontSize: 14),
                    )
                  ],
                ),
              ),
            ),
          );
  }
}

`如果有人指出我的错误,那将是很有意义的. 预先谢谢你.

`it would be appreciable if someone point the mistake i did. thank you in advance.

推荐答案

[已解决]

之间的距离

GeolocatorPlatform.distanceBetween(startLatitude, startLongitude, endLatitude, endLongitude);

对于轴承距离

GeolocatorPlatform.bearingBetween(startLatitude, startLongitude, endLatitude, endLongitude);

在撰写此答案时,官方文档不正确.我尝试了以上两个其他答案,但如果您遇到这种情况,请尝试使用对我有用的这些代码,并希望它也能解决您的问题. [版本:geolocator 6.0.0 + 1]

At the time of writing this answer official documentation is not correct. I tried both other answers above and not worked if that's the case with you try these code it worked for me and hopefully this will fix your problem too. [ version : geolocator 6.0.0+1 ]

git https://github.com/Baseflow/flutter- geolocator/issues/496#issuecomment-683861804

这篇关于geolocator 6.0.0 + 1 distanceBetween()在颤动中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆