0

我有点卡住了,我创建了一个接收字符串值(岛屿名称)的类“CoordXYcenter”,我的方法测试名称以定义我的纬度和经度。

class Maposm extends StatefulWidget {

  final island;
  Maposm(this.island);
  @override
  _MaposmState createState() => _MaposmStat(island);
}

class _MaposmState extends State<Maposm> {

  final island;
  _MaposmState(this.island);
  double _lat= CoordXYcenterisland(island:island).centerlat; // Pb here
  double _long= CoordXYcenterisland(island:island).centerlong;//same

  @override
  Widget build(BuildContext context) {
  return new FlutterMap(
    options: new MapOptions(
      center: new LatLng(_lat ,_long),

我有一条错误消息:未定义命名参数“岛”。但是 island 是在另一个类中定义的,用户按下一个按钮来定义值

4

2 回答 2

0

是的,这是我在第 20 行和第 21 行收到的完整错误消息:无法在初始化程序中访问实例成员“岛”。尝试用不同的表达式替换对实例成员的引用 [20,36]

import 'dart:core';
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong/latlong.dart';
import 'package:algua_alpha/choose_island.dart';

class Maposm extends StatefulWidget {

  final island;
  Maposm(this.island);
  @override
  _MaposmState createState() => _MaposmState(island);
}

class _MaposmState extends State<Maposm> {

  final island;
  _MaposmState(this.island);
  double _lat= CoordXYcenterisland(island).centerlat;//line 20 error
  double _long= CoordXYcenterisland(island).centerlong;//line 21 error

  @override
  Widget build(BuildContext context) {
  return new FlutterMap(
    options: new MapOptions(
      center: new LatLng(_lat ,_long),
      zoom: 10.4,
    ),
    layers: [
      new TileLayerOptions(
       urlTemplate: "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
        subdomains: ['a', 'b', 'c']
      ),
      // new MarkerLayerOptions(
      //   markers: [
      //     new Marker(
      //       width: 80.0,
      //       height: 80.0,
      //       point: new LatLng(51.5, -0.09),
      //       builder: (ctx) =>
      //       new Container(
      //         child: new FlutterLogo(),
      //       ),
      //     ),
      //   ],
      // ),
    ],
  );
}
}
于 2020-09-22T07:51:53.263 回答
0

哎呀我的错误旧代码,我已经修改:CoordXYcenterisland(岛),

class CoordXYcenterisland {
  final String island;    
  double centerlat,centerlong;

  CoordXYcenterisland(this.island);

    getcenterxy(){  
        if(island.toLowerCase()=='guadeloupe'){
          centerlat=16.120719;
          centerlong=-61.505664;
        }
        else if (island.toLowerCase()=='martinique'){
          centerlat=14.647310;
          centerlong= -60.998896;
        }
        else {      
          centerlat=15.120719;
          centerlong=-61.25;
          }
          return;
    }
}

但是我仍然得到“无法在初始化程序中访问”,是因为我没有从我的方法中返回两个坐标吗?

于 2020-09-21T20:42:21.627 回答