Geolocation.getCurrentPosition 来自 dart:html 无法在启用 NNBD 的发布模式下工作
Geolocation.getCurrentPosition from dart:html not working in release mode with NNBD enabled
有人知道为什么 dart:html
中的 Geolocation.getCurrentPosition()
在 运行 处于启用声音空安全的释放模式时不起作用 (flutter run --release -d chrome
) 吗?
进行此调用时,我确实得到了 Geoposition
的实例,但是当我尝试访问其中一个成员时,我收到以下错误消息:
Uncaught TypeError: n.grq is not a function
at main.dart:39
at WD.a (async_patch.dart:316)
at WD. (async_patch.dart:341)
at VR. (async_patch.dart:292)
at UG.SB (zone.dart:1612)
at UG.ug (zone.dart:1611)
at Tf.[=12=] (future_impl.dart:118)
at Object.n3 (future_impl.dart:733)
at Y.ib (future_impl.dart:539)
at T7.[=12=] (future_impl.dart:577)
如果我 运行 在调试中使用相同的代码(flutter run -d chrome
或没有 NNBD flutter run -d chrome --release --no-sound-null-safety
一切正常)。重现此行为的简单应用程序如下所示(它是默认 Flutter 计数器模板的略微更改版本):
import 'dart:html' as html;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, this.title}) : super(key: key);
final String? title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _position = "Unknown";
Future<void> _currentPosition() async {
final geolocation = html.window.navigator.geolocation;
final position = await geolocation.getCurrentPosition();
final latitude = position.coords?.latitude ?? 'n/a';
final longitude = position.coords?.longitude ?? 'n/a';
setState(() {
_position =
'Latitude: $latitude, Longitude: $longitude';
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title!),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Your current position is:',
),
Text(
'$_position',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _currentPosition,
tooltip: 'Current position',
child: Icon(Icons.location_on),
),
);
}
}
事实证明这是 Dart 中的一个错误。我创建了一个错误报告,可以在此处进行跟踪:https://github.com/dart-lang/sdk/issues/45562
有人知道为什么 dart:html
中的 Geolocation.getCurrentPosition()
在 运行 处于启用声音空安全的释放模式时不起作用 (flutter run --release -d chrome
) 吗?
进行此调用时,我确实得到了 Geoposition
的实例,但是当我尝试访问其中一个成员时,我收到以下错误消息:
Uncaught TypeError: n.grq is not a function
at main.dart:39
at WD.a (async_patch.dart:316)
at WD. (async_patch.dart:341)
at VR. (async_patch.dart:292)
at UG.SB (zone.dart:1612)
at UG.ug (zone.dart:1611)
at Tf.[=12=] (future_impl.dart:118)
at Object.n3 (future_impl.dart:733)
at Y.ib (future_impl.dart:539)
at T7.[=12=] (future_impl.dart:577)
如果我 运行 在调试中使用相同的代码(flutter run -d chrome
或没有 NNBD flutter run -d chrome --release --no-sound-null-safety
一切正常)。重现此行为的简单应用程序如下所示(它是默认 Flutter 计数器模板的略微更改版本):
import 'dart:html' as html;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, this.title}) : super(key: key);
final String? title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _position = "Unknown";
Future<void> _currentPosition() async {
final geolocation = html.window.navigator.geolocation;
final position = await geolocation.getCurrentPosition();
final latitude = position.coords?.latitude ?? 'n/a';
final longitude = position.coords?.longitude ?? 'n/a';
setState(() {
_position =
'Latitude: $latitude, Longitude: $longitude';
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title!),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Your current position is:',
),
Text(
'$_position',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _currentPosition,
tooltip: 'Current position',
child: Icon(Icons.location_on),
),
);
}
}
事实证明这是 Dart 中的一个错误。我创建了一个错误报告,可以在此处进行跟踪:https://github.com/dart-lang/sdk/issues/45562