【问题标题】:Issue with initializing. How can I convert something like this to a static member? if that's what I need to do初始化问题。我怎样才能将这样的东西转换为静态成员?如果那是我需要做的
【发布时间】:2020-08-08 23:06:09
【问题描述】:

我正在尝试从文本输入(纬度、经度、半径)传递数据来绘制一个圆:

Map<double, double> n1 = (platitude)
    .map((key, value) => MapEntry(double.parse(key), double.parse(value)));
List longitudelist = platitude.values.toList();
    
    
Future<void> _makeCircles() async {
 setState(() {
   circles = Set.from([
     Circle(
         circleId: CircleId("none"),
         center: LatLng(platitude, plongitude),
         radius: radius,
         fillColor: Color.fromRGBO(255, 255, 255, .5),
         strokeColor: Color.fromRGBO(247, 16, 0, .4))
   ]);
 });
}

我未能按照 center: LatLng 的要求转换为双精度值,因为这些值不是静态的。有人知道解决方法吗?

编辑:

根据要求,我添加了更多代码以使其更具重现性:

这就是我从用户输入中获取纬度、经度和半径作为字符串的方式:

TextFormField(
                          style: TextStyle(
                            color: Colors.white,
                          ),
                          initialValue: "33.2038241",
                          decoration: InputDecoration(
                            hintText: 'Enter latitude',
                          ),
                          onChanged: (String value) {
                            platitude = value;
                          },
                        ),

您可以忽略初始值,该值仅用于测试。 现在,我正在尝试将该字符串传递给 Geolocator 的center: LatLng(platitude, plongitude)。如上图。

这需要双精度值。我尝试像这样转换为双精度:

  var platitude = double.parse(platitude);

这不起作用。 “只有静态成员可以作为初始化器访问。”

所以,我做了一些研究并尝试像这样在 initstate 中调用它:

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {

  @override
  void initState() {
    super.initState();
    getLocation();

    platitude = new TextFormField(
      keyboardType: TextInputType.emailAddress,
      controller: filterController);
    plongitude = new TextFormField(
      keyboardType: TextInputType.emailAddress,
      controller: filterController);
    radius = new TextFormField(
      keyboardType: TextInputType.emailAddress,
      controller: filterController);

  }

  double n1 = double.parse(platitude); 
  
  TabController _tabController;
  final filterController = new TextEditingController(text: "Search");

  TextFormField platitude;
  TextFormField plongitude;
  TextFormField radius;

这导致“无法为参数 TextFormField 分配字符串”和“只能访问静态成员...”

有谁知道我怎样才能做到这一点? :/

【问题讨论】:

  • 是什么让您认为您需要static 会员?您能否提供一个可重复的示例来说明您想要什么?很难为您提供帮助,因为您没有显示很多代码,并且您假设人们知道 LatLngCircle 是什么。
  • 我收到的错误表明“只有静态成员可以作为初始化程序访问”。我需要将双精度传递给 LatLng,因为 Flutter 的 Geolocator 期望这样:中心:LatLng(纬度,经度)。我的 lat 和 long 来自用户输入字符串。我尝试像这样转换: var platitude = double.parse(platitude);并得到上述错误。我也尝试调用 initState /w SingleTickerProviderStateMixin 但它没有用.. :/
  • @jamesdlin 已添加。

标签: google-maps flutter dart


【解决方案1】:

错误告诉你不能这样做:

  double n1 = double.parse(platitude);

类定义中,因为platitude 不是static。类定义中非static 成员的内联初始化程序(不是late)在对象构造时执行。从其他非static 成员初始化非static 成员没有意义,因为那些其他成员尚未初始化。更具体地说,在您的示例中,platitude 直到 initState 被调用后才会被初始化。

一般来说,如果您想从另一个非static 成员初始化一个非static 成员,只需将初始化移动到其他地方。由于n1 依赖于platitude,因此在initState 中初始化platitude 之后立即初始化n1 是有意义的。

也就是说,在您的情况下,这也没有意义。您可能不想在创建 empty TextFormField 后立即对其进行初始化。此外,double.parse 需要 String 参数,而不是 TextFormField

你可能想做一些类似的事情:

  @override
  void initState() {
    ...

    platitude = TextFormField(
      ...
      onSaved: (String value) => n1 = double.parse(value),
    );

(在initState 中进行这项工作也很奇怪;通常您只需在build 方法中构造诸如TextFormField 之类的小部件。)

【讨论】:

    【解决方案2】:

    更新:- 改为这样做,

    var latitude, longitude;
    Future<void> _makeCircles() async {
     setState(() {
       circles = Set.from([
         Circle(
             circleId: CircleId("none"),
             center: LatLng(double.parse(latitude), double.parse(longitude)),
             radius: radius,
             fillColor: Color.fromRGBO(255, 255, 255, .5),
             strokeColor: Color.fromRGBO(247, 16, 0, .4))
       ]);
     });
    }
    

    而且,在您的 TextFormFields 中,

    TextFormField(
                              style: TextStyle(
                                color: Colors.white,
                              ),
                              initialValue: "33.2038241",
                              decoration: InputDecoration(
                                hintText: 'Enter latitude',
                              ),
                              onChanged: (String value) {
                                latitude=value;
                              },
                            ),
    

    现在,您可以在需要时调用 _makeCircles() 方法,但请确保始终在初始化纬度和经度后调用该方法,因为如果在此之前调用该方法,则会引发 noSuchMethod 错误。

    【讨论】:

    • 对经度做同样的事情。
    • 如果有帮助,请点赞并接受这个答案:)
    • 你不能从自身初始化一个变量。即使假设您的意思是static var n1 = double.parse(platitude);,那仍然是错误的,因为platitude 必须是static。即使假设您的意思是static TextFormField platitude,它也回答了X-Y problem 的错误部分:是的,这将避免错误消息,但该成员是static 没有意义。
    • 是的,我注意到我的错误^_^',顺便感谢您的解释。
    猜你喜欢
    • 1970-01-01
    • 2022-11-29
    • 2021-07-12
    • 2022-01-12
    • 1970-01-01
    • 2021-02-02
    • 1970-01-01
    • 2011-08-20
    • 2021-11-07
    相关资源
    最近更新 更多