FLUTTER-获得位置权限后,Futurebuilder始终返回null

时间:2019-10-29 18:43:01

标签: flutter

问题

在用户授予访问其位置的权限后,Futurebuilder始终返回“ null”,以便它可以计算2个位置之间的距离。

我想要它做什么

刷新页面时确实会提供位置,但是我希望用户在访问时提供2个对象之间的距离,而不是在刷新页面时。

运行应用的主要代码

import 'package:flutter/material.dart';
import 'mainlist.dart';




void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override

  Widget build(BuildContext context) {

    return new MaterialApp(
      routes: {
        '/second': (context) => mainlist()
      },
      title: "testapp",
      debugShowCheckedModeBanner: false,
      home: mainlist(),
    );
  }
}

发生问题的代码-Futurebuilder + getCurrenPosition Future

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';

import 'package:async/async.dart';
import 'package:geolocator/geolocator.dart';
import 'package:permission/permission.dart';
import 'mainlist.dart';


class mainlist extends StatefulWidget {
  @override
  _mainlistpage createState() => _mainlistpage();
}


class _mainlistpage extends State<mainlist> {


  Future<String> getCurrentPosition(DocumentSnapshot document) async{
    Position position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    double distanceInMeters = await Geolocator().distanceBetween(position.latitude, position.longitude, document['lat'], document['lat']);
    return distanceInMeters.toString();
  }

  var sortBy = "";

  Widget homePage() {
    return StreamBuilder(
      stream: Firestore.instance.collection("Test").orderBy(sortBy).snapshots(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) return Text("Loading");
        return ListView.builder(
            itemCount: snapshot.data.documents.length,
            itemBuilder: (context, index) =>
                _mainListItem(context, snapshot.data.documents[index]));
      },
    );
  }


  @override
  Widget _mainListItem(BuildContext context, DocumentSnapshot document) {
    return Scaffold(
      body: Container(
        child: Center(
          child: Column(
              children: <Widget>[
                FutureBuilder(
                    future: getCurrentPosition(document),
                    builder: (BuildContext context,AsyncSnapshot snapshot)  {
                      switch (snapshot.connectionState) {
                        case ConnectionState.none:
                          return Text('none');
                        case ConnectionState.active:
                        case ConnectionState.waiting:
                          return Text('waiting');
                        case ConnectionState.done:
                          if (snapshot.hasError)
                            return Text('Error: ${snapshot.error}');
                          return Text(snapshot.data.toString());

                      }
                      return null; // unreachable
                    }
                ),
              ]
          ),
        ),
      ),
    );
  }

  Widget build(BuildContext context){
    return new Scaffold();
  }
}

我尝试了什么

  • 使用streambuilder
  • 在Futurebuilder中处理案例
  • 阅读Stackoverflow
  • 我已在plist和manifest中添加了权限

0 个答案:

没有答案