1

有谁知道为什么在启用了声音空安全性的发布模式下运行时Geolocation.getCurrentPosition()fromdart:html不起作用(flutter run --release -d chrome)?

进行此调用时,我确实得到了一个Geopositionback 实例,但是当我尝试访问其中一个成员时,我收到以下错误消息:

Uncaught TypeError: n.grq is not a function
    at main.dart:39
    at WD.a (async_patch.dart:316)
    at WD.$2 (async_patch.dart:341)
    at VR.$1 (async_patch.dart:292)
    at UG.SB (zone.dart:1612)
    at UG.ug (zone.dart:1611)
    at Tf.$0 (future_impl.dart:118)
    at Object.n3 (future_impl.dart:733)
    at Y.ib (future_impl.dart:539)
    at T7.$0 (future_impl.dart:577)

如果我在调试中运行相同的代码(flutter run -d chrome或没有 NNBD,flutter run -d chrome --release --no-sound-null-safety一切正常)。重现此行为的简单 App 如下所示(它是默认 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),
      ),
    );
  }
}
4

1 回答 1

0

这原来是 Dart 中的一个错误。我创建了一个可以在此处跟踪的错误报告:https ://github.com/dart-lang/sdk/issues/45562

于 2021-04-08T16:42:48.093 回答