Flutter 如何在Dart中获得车辆的速度
我在Dart有一个小项目,我在那里使用库Flutter 如何在Dart中获得车辆的速度,flutter,dart,Flutter,Dart,我在Dart有一个小项目,我在那里使用库位置。 有了这个库,我试着用GPS定位来计算车辆的速度。 问题是每次运行项目时,控制台中都会出现错误: The following NoSuchMethodError was thrown building ListenLocationWidget(dirty, state: _ListenLocationState#55ce8): The getter 'speed' was called on null. Receiver: null Tried ca
位置
。
有了这个库,我试着用GPS定位来计算车辆的速度。
问题是每次运行项目时,控制台中都会出现错误:
The following NoSuchMethodError was thrown building ListenLocationWidget(dirty, state: _ListenLocationState#55ce8):
The getter 'speed' was called on null.
Receiver: null
Tried calling: speed
以下是我的小项目的链接:
这是我的代码,我在其中计算位置之间的速度:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:location/location.dart';
class ListenLocationWidget extends StatefulWidget {
const ListenLocationWidget({Key key}) : super(key: key);
@override
_ListenLocationState createState() => _ListenLocationState();
}
class _ListenLocationState extends State<ListenLocationWidget> {
final Location location = Location();
LocationData _location;
StreamSubscription<LocationData> _locationSubscription;
String _error;
@override
void initState() {
super.initState();
_listenLocation();
}
Future<void> _listenLocation() async {
_locationSubscription =
location.onLocationChanged.handleError((dynamic err) {
setState(() {
_error = err.code;
});
_locationSubscription.cancel();
}).listen((LocationData currentLocation) {
setState(() {
_error = null;
_location = currentLocation;
});
});
}
Future<void> _stopListen() async {
_locationSubscription.cancel();
}
Widget _calculateSpeedBetweenLocations() {
return Text(
'${_location.speed != null && _location.speed * 3600 / 1000 > 0 ? (_location.speed * 3600 / 1000).toStringAsFixed(2) : 0} KM/h',
style: TextStyle(
color: Colors.lightGreen[500],
fontSize: 20,
letterSpacing: 4,
),
);
}
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
_calculateSpeedBetweenLocations(),
],
);
}
}
导入'dart:async';
进口“包装:颤振/材料.省道”;
导入“package:location/location.dart”;
类ListenLocationWidget扩展StatefulWidget{
const ListenLocationWidget({Key}):super(Key:Key);
@凌驾
_ListenLocationState createState()=>\u ListenLocationState();
}
类_ListenLocationState扩展状态{
最终位置=位置();
位置数据\u位置;
StreamSubscription _locationSubscription;
字符串错误;
@凌驾
void initState(){
super.initState();
_listenLocation();
}
Future _listenLocation()异步{
_位置订阅=
location.onLocationChanged.handleError((动态错误){
设置状态(){
_error=err.code;
});
_locationSubscription.cancel();
}).侦听((位置数据当前位置){
设置状态(){
_错误=null;
_位置=当前位置;
});
});
}
Future\u stopListen()异步{
_locationSubscription.cancel();
}
小部件\u calculateSpeedBetweenLocations(){
返回文本(
“${u location.speed!=null&&{u location.speed*3600/1000>0”({u location.speed*3600/1000).toStringAsFixed(2):0}KM/h”,
样式:TextStyle(
颜色:颜色。浅绿色[500],
尺寸:20,
字母间距:4,
),
);
}
@凌驾
小部件构建(构建上下文){
返回列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
_计算位置之间的种子(),
],
);
}
}
这是我的主屏幕:
import 'package:vehicle_speed_demo/location/permissionStatus.dart';
import 'package:vehicle_speed_demo/location/serviceEnabled.dart';
import 'package:vehicle_speed_demo/location/getLocation.dart';
import 'package:vehicle_speed_demo/location/listenLocation.dart';
import 'package:flutter/material.dart';
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: Colors.grey[900],
appBar: _appBar(),
body: _buildUI(),
),
);
}
Widget _appBar() {
return AppBar(
backgroundColor: Colors.purple[800],
elevation: 0.0,
title: Text(
'My App',
style: TextStyle(
color: Colors.white,
fontSize: 30.0,
),
),
automaticallyImplyLeading: false,
);
}
Widget _buildUI() {
return Container(
child: Padding(
padding: const EdgeInsets.all(10),
child: Column(
children: <Widget>[
Wrap(
spacing: 30.0, // gap between sections
runSpacing: 30.0, // gap between lines
alignment: WrapAlignment.spaceAround,
children: <Widget>[
Divider(color: Colors.white, height: 20),
_buildSpeedSection(),
],
),
],
),
));
}
Widget _buildSpeedSection() {
return Container(
height: 150,
width: 170,
child: Column(
children: <Widget>[
Text(
'SPEED',
style: TextStyle(
color: Colors.white,
fontSize: 30,
),
),
PermissionStatusWidget(),
ServiceEnabledWidget(),
GetLocationWidget(),
ListenLocationWidget(),
],
),
);
}
}
import'程序包:车辆\速度\演示/位置/许可状态.dart';
导入“包:车辆\速度\演示/位置/服务启用。dart”;
导入“包:车辆\速度\演示/位置/获取位置.省道”;
导入“包装:车辆\速度\演示/位置/列表位置.dart”;
进口“包装:颤振/材料.省道”;
类主屏幕扩展StatefulWidget{
@凌驾
_HomeScreenState createState()=>\u HomeScreenState();
}
类_homescrenstate扩展状态{
@凌驾
void initState(){
super.initState();
}
@凌驾
无效处置(){
super.dispose();
}
小部件构建(构建上下文){
返回材料PP(
debugShowCheckedModeBanner:false,
家:脚手架(
resizeToAvoidBottomInset:false,
背景颜色:颜色。灰色[900],
appBar:_appBar(),
正文:_buildUI(),
),
);
}
小部件_appBar(){
返回AppBar(
背景颜色:颜色.紫色[800],
标高:0.0,
标题:正文(
“我的应用程序”,
样式:TextStyle(
颜色:颜色,白色,
字体大小:30.0,
),
),
自动嵌入:false,
);
}
小部件_buildUI(){
返回容器(
孩子:填充(
填充:常量边集。全部(10),
子:列(
儿童:[
包裹(
间距:30.0,//截面之间的间隙
行间距:30.0,//行间距
对齐:wrappalignment.spaceAround,
儿童:[
分隔器(颜色:Colors.white,高度:20),
_buildSpeedSection(),
],
),
],
),
));
}
小部件_buildSpeedSection(){
返回容器(
身高:150,
宽度:170,
子:列(
儿童:[
正文(
“速度”,
样式:TextStyle(
颜色:颜色,白色,
尺寸:30,
),
),
PermissionStatusWidget(),
ServiceEnabledWidget(),
GetLocationWidget(),
ListenLocationWidget(),
],
),
);
}
}
任何帮助都将不胜感激 可能是_location.speed导致了问题,因为最初_location为空。未来需要一些时间来执行,因此总结一下,您正在尝试访问_location的speed属性,该属性在开始时为null 试试这个:
Widget _calculateSpeedBetweenLocations() {
// Check if location is null
if(_location == null) return Text("Hold on!");
return Text(
'${_location.speed != null && _location.speed * 3600 / 1000 > 0 ? (_location.speed * 3600 / 1000).toStringAsFixed(2) : 0} KM/h',
style: TextStyle(
color: Colors.lightGreen[500],
fontSize: 20,
letterSpacing: 4,
),
);
您可能还想看看,因为您的问题是您没有等待异步调用完成,所以在使用变量时仍然为null。谢谢您,先生!如果解决了我的问题,那就
。