Flutter 未来执行后不显示窗口小部件
安卓工作室4 颤振工程 下面是一段:Flutter 未来执行后不显示窗口小部件,flutter,dart,Flutter,Dart,安卓工作室4 颤振工程 下面是一段: import 'package:flutter/cupertino.dart'; import 'package:geolocator/geolocator.dart'; import 'package:permission_handler/permission_handler.dart'; Future<bool> isGeolocationStatusDisabled() async { GeolocationStatus geolo
import 'package:flutter/cupertino.dart';
import 'package:geolocator/geolocator.dart';
import 'package:permission_handler/permission_handler.dart';
Future<bool> isGeolocationStatusDisabled() async {
GeolocationStatus geolocationStatus = await getGeolocationStatus();
return (geolocationStatus == GeolocationStatus.disabled);
}
Widget _createMapInnerContainerLeft() {
_logger.d("_createMapInnerContainerLeft: -> call_async_isGeolocationStatusDisabled()");
return _createTurnOffGlobalLocationServiceContainerLeft();
}
Widget _createTurnOffGlobalLocationServiceContainerLeft() {
_logger.d("_createTurnOffGlobalLocationServiceContainerLeft:");
return new Container(
margin: const EdgeInsets.only(
left: Constants.DEFAULT_MARGIN, top: Constants.DEFAULT_MARGIN),
child:
new Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
new Text("Geolocation service is turned off",
style: _createNearestGazStationTextViewStyle(FontWeight.bold)),
new Text("Turn on geolocation for the application",
style: _createNearestGazStationTextViewStyle()),
new Align(
alignment: Alignment.center,
child: new Padding(
padding: EdgeInsets.only(top: Constants.DEFAULT_MARGIN),
child: new RaisedButton(
onPressed: () {
_logger.d("click");
},
child: Text('Settings'.toUpperCase(),
style: _createNearestGazStationTextViewStyle()),
color: new Color(Constants.COLOR_PRIMARY),
textColor:
new Color(Constants.COLOR_PRIMARY_TEXT_COLOR))))
]));
}
现在小部件不显示:
获得空白的原因是函数返回
null
,因为这是Dart
函数的默认行为,函数不会在所有条件路径上返回值
未处理条件路径的原因是,您没有使用await
关键字等待将来的执行,因此程序开始执行isGeolocationStatusDisabled
函数,但不等待结果
您应该使用FutureBuilder
根据Future
执行的结果呈现Widget
Widget _createMapInnerContainerLeft() {
return FutureBuilder(
future: isGeolocationStatusDisabled(),
builder: (context, futureData) {
if (futureData.hasData) {
_logger.d("_createMapInnerContainerLeft: -> call_async_isGeolocationStatusDisabled()");
bool isGeolocationStatusDisabledFuture = futureData.data;
if (isGeolocationStatusDisabledFuture) {
return _createTurnOffGlobalLocationServiceContainerLeft();
}
}
return CircularProgressIndicator();
}
)
}
查看页面以了解有关FutureBuilder
的更多信息。您还可以阅读有关Dart的更多信息,如异步编程,此帮助:
Widget _createMapInnerContainerLeft() {
_logger.d(
"_createMapInnerContainerLeft: -> call_async_isGeolocationStatusDisabled()");
return FutureBuilder(
future: isGeolocationStatusDisabled(),
builder: (context, projectSnap) {
ConnectionState connectionState = projectSnap.connectionState;
bool hasData = projectSnap.hasData;
_logger.d("_createMapInnerContainerLeft: connectionState = $connectionState, hasData = $hasData");
if (connectionState == ConnectionState.done && hasData != null) {
_logger.d("_createMapInnerContainerLeft: -> call_showNearestGazStationViewMode");
return _showNearestGazStationViewMode();;
}
_logger.d("_createMapInnerContainerLeft: -> call_Container()");
return Container();
});
}
Future<bool> isGeolocationStatusDisabled() async {
GeolocationStatus geolocationStatus = await getGeolocationStatus();
return (geolocationStatus == GeolocationStatus.disabled);
}
Widget\u createMapinerContainerLeft(){
_记录器.d(
“\u createMapinerContainerLeft:->调用\u async\u isGeolocationStatusDisabled()”;
回归未来建设者(
未来:isGeolocationStatusDisabled(),
生成器:(上下文,projectSnap){
ConnectionState ConnectionState=projectSnap.ConnectionState;
bool hasData=projectSnap.hasData;
_d(“_createMapinerContainerLeft:connectionState=$connectionState,hasData=$hasData”);
if(connectionState==connectionState.done&&hasData!=null){
_logger.d(“\u createMapinerContainerLeft:->调用\u showNearestGazStationViewMode”);
返回_showNearestGazStationViewMode();;
}
_logger.d(“\u createMapinerContainerLeft:->call\u Container()”;
返回容器();
});
}
未来isGeolocationStatusDisabled()异步{
GeolocationStatus GeolocationStatus=等待getGeolocationStatus();
返回(geolocationStatus==geolocationStatus.disabled);
}
您应该使用@Neil的答案,但您应该注意编译器警告/信息。您应该已经看到了关于该函数的一些内容,它不是以return
语句结尾的。
Widget _createMapInnerContainerLeft() {
_logger.d(
"_createMapInnerContainerLeft: -> call_async_isGeolocationStatusDisabled()");
return FutureBuilder(
future: isGeolocationStatusDisabled(),
builder: (context, projectSnap) {
ConnectionState connectionState = projectSnap.connectionState;
bool hasData = projectSnap.hasData;
_logger.d("_createMapInnerContainerLeft: connectionState = $connectionState, hasData = $hasData");
if (connectionState == ConnectionState.done && hasData != null) {
_logger.d("_createMapInnerContainerLeft: -> call_showNearestGazStationViewMode");
return _showNearestGazStationViewMode();;
}
_logger.d("_createMapInnerContainerLeft: -> call_Container()");
return Container();
});
}
Future<bool> isGeolocationStatusDisabled() async {
GeolocationStatus geolocationStatus = await getGeolocationStatus();
return (geolocationStatus == GeolocationStatus.disabled);
}