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