【问题标题】:Error: A non-null value must be returned since the return type 'Widget' doesn't allow null错误:必须返回非空值,因为返回类型 'Widget' 不允许为空
【发布时间】:2021-09-02 23:26:56
【问题描述】:

使用 FutureBuilder,我收到以下错误。

错误:无法分配参数类型“AsyncSnapshot” 到参数类型“位置”。
指向线:return Map(snapshot);

错误:必须返回非空值,因为返回类型 'Widget' 不允许为空。 指向行:builder: (context, snapshot)

home.dart

 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!);

                   }

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

在构建 FutureBuilder>>>>(dirty, state: _FutureBuilderState#55f91) 时抛出了以下 _CastError: 用于空值的空检查运算符

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

【问题讨论】:

    标签: flutter flutter-futurebuilder


    【解决方案1】:

    您需要在 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(); 
          }
       }
     ),
    );
    

    【讨论】:

    • 谢谢!做到了!
    猜你喜欢
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    • 2022-07-26
    • 1970-01-01
    • 2022-01-15
    相关资源
    最近更新 更多