颤振指南针示例

时间:2021-05-25 09:32:09

标签: flutter dart location compass direction

希望大家都过得好。我正在使用颤振(作为初学者),我想创建一个简单的指南针应用程序。我安装了 flutter_compass 并使用包提供的示例来测试它,但它卡在无限加载上。有什么办法可以解决这个问题吗?

注意:我注意到代码卡在 connectState == wating 上。

Full code

The example link

  Widget _buildCompass() {
    return StreamBuilder<CompassEvent>(
      stream: FlutterCompass.events,
      builder: (context, snapshot) {
        if (snapshot.hasError) {
          return Text('Error reading heading: ${snapshot.error}');
        }

        if (snapshot.connectionState == ConnectionState.waiting) {
          return Center(
            child: CircularProgressIndicator(),
          );
        }

        double? direction = snapshot.data!.heading;

        // if direction is null, then device does not support this sensor
        // show error message
        if (direction == null)
          return Center(
            child: Text("Device does not have sensors !"),
          );

        return Material(
          shape: CircleBorder(),
          clipBehavior: Clip.antiAlias,
          elevation: 4.0,
          child: Container(
            padding: EdgeInsets.all(16.0),
            alignment: Alignment.center,
            decoration: BoxDecoration(
              shape: BoxShape.circle,
            ),
            child: Transform.rotate(
              angle: (direction * (math.pi / 180) * -1),
              child: Image.asset('assets/compass.jpg'),
            ),
          ),
        );
      },
    );
  }

2 个答案:

答案 0 :(得分:0)

你更新你的安卓清单文件了吗?

<块引用>

iOS 确保将带有适当描述的键添加到 Info.plist 文件。

NSLocationWhenInUseUsageDescription NSLocationAlwaysAndWhenInUseUsageDescription ? 参考示例代码

Android 确保添加权限 app/src/main/AndroidManifest.xml 文件。

android.permission.INTERNET android.permission.ACCESS_COARSE_LOCATION android.permission.ACCESS_FINE_LOCATION

答案 1 :(得分:0)

我刚刚了解到你不能在 IOS 模拟器上使用 flutter compass 包,因为它没有包正常工作所需的传感器,这就是为什么它卡在无限加载屏幕上(connectState == waiting),所以你只需要在真正的 Iphone 设备上测试它就可以了。