Mobile data connect to the internet but wifi does not work in release build.

huangapple go评论59阅读模式
英文:

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

&lt;manifest xmlns:android...&gt;
 ...
 &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;
 &lt;application ...
&lt;/manifest&gt;

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&lt;String&gt; yourData() async {
 // Do some asynchronous work here
 return &#39;My data&#39;;
}

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.
  });
}

huangapple
  • 本文由 发表于 2023年5月11日 00:01:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220523.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定