英文:
Mobile data connect to the internet but wifi does not work in release build
问题
它在调试时可以使用移动数据和Wi-Fi,但在发布模式下在应用程序中无法工作。在发布模式下,它显示了一个错误,内容为:Future.then必须返回返回的Future类型的值
。
英文:
It works on mobile data and wifi during debug but on release mode it does not work in the app. It shows an error of Future.then must return a value of the returned future's type
in release mode
答案1
得分: 1
自从您说它在调试模式下运行正常,但在发布模式下不运行:
Flutter会自动在AndroidManifest中设置Internet权限,但您需要为发布版本手动设置。
https://docs.flutter.dev/data-and-backend/networking
<manifest xmlns:android...>
...
<uses-permission android:name="android.permission.INTERNET" />
<application ...
</manifest>
将此添加到您的AndroidManifest.xml文件中。
英文:
Since you said it works in debug, but not in release:
Flutter sets the Internet Permission in the AndroidManifest automatically, but you have to set it yourself for the release version.
https://docs.flutter.dev/data-and-backend/networking
<manifest xmlns:android...>
...
<uses-permission android:name="android.permission.INTERNET" />
<application ...
</manifest>
Add this to your AndroidManifest.xml
答案2
得分: 0
为了解决这个错误,您需要确保Future对象上的then方法返回了正确类型的值。
以下是如何解决这个错误的示例:
Future<String> yourData() async {
// 在这里执行一些异步工作
return 'My data';
}
void main() {
// 这在调试模式下可以工作
getMyData().then((value) {
print(value);
});
// 这在发布模式下会抛出错误
yourData().then((value) {
print(value as int); // 抛出错误,因为值是一个字符串。
});
}
英文:
To fix this error, you need to make sure that the then method on the Future object is returning the correct type of value.
Here is an example of how to fix this error:
Future<String> yourData() async {
// Do some asynchronous work here
return 'My data';
}
void main() {
// This will work in debug mode
getMyData().then((value) {
print(value);
});
// This will throw an error in release mode
yourData().then((value) {
print(value as int); // Throw an error because the value is a String.
});
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论