简体   繁体   English

Flutter Stateful Constructor Const问题

[英]Flutter Stateful Constructor Const Problem

I'm following tutorial on udemy and i found this weird error and i already read some doccumentation and i still didnt find the right answer to have constructor with initail value.我正在关注 udemy 上的教程,我发现了这个奇怪的错误,我已经阅读了一些文档,但我仍然没有找到正确的答案来让构造函数具有初始值。 her is my code她是我的密码

class MapScreen extends StatefulWidget {

      final PlaceLocation initialLocation;
      final bool isSelecting;
      const MapScreen({Key? key, this.initialLocation = PlaceLocation( //here there is an error "The default value of an optional parameter must be constant. (Documentation)"
        latittude:  37.422,
        longitude: -122.084,
        address: "Example stree no 1",
      ), this.isSelecting = false}) : super(key: key);
    
      @override
      _MapScreenState createState() => _MapScreenState();
    }

however if i delete those const new error come out但是,如果我删除那些常量,则会出现新错误

    class MapScreen extends StatefulWidget {
      final PlaceLocation initialLocation;
      final bool isSelecting;
      MapScreen({Key? key, this.initialLocation = PlaceLocation( //The default value of an optional parameter must be constant. (Documentation)
        latittude:  37.422,
        longitude: -122.084,
        address: "Jl. Imambonjol",
      ), this.isSelecting = false}) : super(key: key);
    
      @override
      _MapScreenState createState() => _MapScreenState();
    }

i also tried to follow the instruction and modify my code like this:我也尝试按照说明修改我的代码,如下所示:

class MapScreen extends StatefulWidget {
  final PlaceLocation initialLocation;
  final bool isSelecting;

  MapScreen({
    this.initialLocation =
        const PlaceLocation(latitude: 37.422, longitude: -122.084),
    this.isSelecting = false,
  });

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

but the error still came out但错误仍然出现

I was able to fix your mistake, below I will attach the solution code and a screenshot of the console:我能够修复你的错误,下面我将附上解决方案代码和控制台屏幕截图:

// Fake model:
class PlaceLocation {
  final double latittude;
  final double longitude;
  final String address;

  // Make this constructor const to solve your problem:
  const PlaceLocation(
      {required this.latittude,
      required this.longitude,
      required this.address});
}

class MapScreen extends StatefulWidget {
  final PlaceLocation initialLocation;
  final bool isSelecting;

  // Also don't forget to put const before PlaceLocation() in this constructor:
  const MapScreen(
      {Key? key,
      this.initialLocation = const PlaceLocation(
        latittude: 37.422,
        longitude: -122.084,
        address: "Example stree no 1",
      ),
      this.isSelecting = false})
      : super(key: key);

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

class _MapScreenState extends State<MapScreen> {
  @override
  Widget build(BuildContext context) {
    // Console test to check default values:
    print("lattitude: " +
        MapScreen().initialLocation.latittude.toString() +
        "\nlongitude: " +
        MapScreen().initialLocation.longitude.toString() +
        "\naddress: " +
        MapScreen().initialLocation.address.toString());

    return Scaffold(body: Container());
  }
}

图像 - 检查默认值

All you have to do is make your model (PlaceLocation) constructor const .您所要做的就是使您的 model (PlaceLocation) 构造函数为const

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM