gpt4 book ai didi

flutter - 错误 : A non-null value must be returned since the return type 'Widget' doesn't allow null

转载 作者:行者123 更新时间:2023-12-05 09:32:29 28 4
gpt4 key购买 nike

使用 FutureBuilder,出现以下错误。

Error: The argument type 'AsyncSnapshot' can't be assignedto the parameter type 'Position'.
Pointing to line: return Map(snapshot);

Error: A non-null value must be returned since the return type 'Widget' doesn't allow null.Pointing to line: builder: ( context, snapshot)

家.飞镖

 Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: FittedBox(
child: Text('something something'),
),
actions: <Widget>[
//irrelevant code for buttons etc

],
body: FutureBuilder <Position> (
future: _geolocatorService.getInitialLocation(),
builder: (BuildContext context, snapshot) {

if (snapshot.hasData) { //<- can confirm snapshot has data and can display it
// printed in terminal ex.: lat value, long,
// null,null

print(snapshot);
return Map(snapshot); //<-having trouble passing snapshot data as Position to
// this map
}
}
),
);

geolocator_service.dart 文件:(导入到 home.dart):

class GeolocatorService {

Stream<Position> getCurrentLocation() {
return Geolocator.getPositionStream(desiredAccuracy: LocationAccuracy.best,distanceFilter: 10);
}

Future <Position> getInitialLocation() async {
return await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.best);
}`

Map.dart文件(导入必要的插件):

class Map extends StatefulWidget {

final Position initialPosition;

Map(this.initialPosition); // <- initialPosition doesnt like the snapshot value

@override
_MapState createState() => _MapState();
}

class _MapState extends State<Map> {

final GeolocatorService geolocatorService = GeolocatorService();

@override


Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: GoogleMap(
initialCameraPosition: CameraPosition(
target: LatLng( widget.initialPosition.latitude, widget.initialPosition.longitude),
zoom: 19),
mapType: MapType.normal,
) ,
)
);

}
}

编辑:如果我删除 if 条件:

body: FutureBuilder <Position> (
future: _geolocatorService.getInitialLocation(),
builder: (context, snapshot) {

return Map(snapshot.data!);

}

它运行了,但是我得到一个简短的红屏错误:

The following _CastError was thrown building FutureBuilder>>>>(dirty, state: _FutureBuilderState#55f91):Null check operator used on a null value

然后照常运行。也许是因为它在等待数据进来?

最佳答案

您需要在 else 条件下返回一个小部件。然后 builder 需要一个小部件,当您添加 if 条件时,它不会返回任何小部件,直到 if 条件获得真值并返回一个小部件。

Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: FittedBox(child: Text('something something')),
body: FutureBuilder <Position> (
future: _geolocatorService.getInitialLocation(),
builder: (BuildContext context, snapshot) {
if (snapshot.hasData) {
//<- can confirm snapshot has data and can display it
// printed in terminal ex.: lat value, long,
// null,null
print(snapshot);
return Map(snapshot);
}else{

/// Display a loader untill data is not fetched from server
return CirclularProgressIndicator();
}
}
),
);

关于flutter - 错误 : A non-null value must be returned since the return type 'Widget' doesn't allow null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68031531/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com