在 android 模拟器 flutter 上构建应用程序时出错
Error wilth building app on android emulator flutter
我不知道出了什么问题,但每次我尝试 运行 模拟器上的这段代码时,它都会在终端中显示错误,但不会 运行,但是一旦我选择chrome 作为测试设备,它运行顺利,我正在尝试使用来自 pub.dev
的地理定位器包
我的代码:
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
class LoadingScreen extends StatefulWidget {
@override
_LoadingScreenState createState() => _LoadingScreenState();
}
class _LoadingScreenState extends State<LoadingScreen> {
void geolocation() async {
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.low,
);
print(position);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () {
geolocation();
//Get the current location
},
child: Text('Get Location'),
),
),
);
}
}
显示的错误:
C:\src\flutter\flutter\.pub-cache\hosted\pub.dartlang.org\geolocator_android-3.0.1\android\src\main\java\com\baseflow\geolocator\location\LocationMapper.java:29: error: cannot find symbol
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
^
symbol: variable S
location: class VERSION_CODES
C:\src\flutter\flutter\.pub-cache\hosted\pub.dartlang.org\geolocator_android-3.0.1\android\src\main\java\com\baseflow\geolocator\location\LocationMapper.java:30: error: cannot find symbol
position.put("is_mocked", location.isMock());
^
symbol: method isMock()
location: variable location of type Location
2 errors
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':geolocator_android:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 27s
Exception: Gradle task assembleDebug failed with exit code 1
将 compileSDKVersion 从 29 更改为 31
android {
compileSdkVersion 31
defaultConfig {
minSdkVersion 21
targetSdkVersion 29
}
}
我不知道出了什么问题,但每次我尝试 运行 模拟器上的这段代码时,它都会在终端中显示错误,但不会 运行,但是一旦我选择chrome 作为测试设备,它运行顺利,我正在尝试使用来自 pub.dev
的地理定位器包我的代码:
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
class LoadingScreen extends StatefulWidget {
@override
_LoadingScreenState createState() => _LoadingScreenState();
}
class _LoadingScreenState extends State<LoadingScreen> {
void geolocation() async {
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.low,
);
print(position);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () {
geolocation();
//Get the current location
},
child: Text('Get Location'),
),
),
);
}
}
显示的错误:
C:\src\flutter\flutter\.pub-cache\hosted\pub.dartlang.org\geolocator_android-3.0.1\android\src\main\java\com\baseflow\geolocator\location\LocationMapper.java:29: error: cannot find symbol
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
^
symbol: variable S
location: class VERSION_CODES
C:\src\flutter\flutter\.pub-cache\hosted\pub.dartlang.org\geolocator_android-3.0.1\android\src\main\java\com\baseflow\geolocator\location\LocationMapper.java:30: error: cannot find symbol
position.put("is_mocked", location.isMock());
^
symbol: method isMock()
location: variable location of type Location
2 errors
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':geolocator_android:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 27s
Exception: Gradle task assembleDebug failed with exit code 1
将 compileSDKVersion 从 29 更改为 31
android {
compileSdkVersion 31
defaultConfig {
minSdkVersion 21
targetSdkVersion 29
}
}