Flutter url_launcher 报错 canLaunch will return false(Android)的解决办法

854 阅读2分钟

这是我参与8月更文挑战的第7天,活动详情查看:8月更文挑战

问题1. Flutter url_launcher 报错 canLaunch will return false(Android)

Starting from API 30 Android requires package visibility configuration in your AndroidManifest.xml otherwise canLaunch will return false. A element must be added to your manifest as a child of the root element.

检查 <项目目录>/android/app/src/main/AndroidManifest.xml 中是否添加了权限:

<queries>
  <!-- If your app opens https URLs -->
  <intent>
    <action android:name="android.intent.action.VIEW" />
    <data android:scheme="https" />
  </intent>
  <!-- If your app makes calls -->
  <intent>
    <action android:name="android.intent.action.DIAL" />
    <data android:scheme="tel" />
  </intent>
  <!-- If your app emails -->
  <intent>
    <action android:name="android.intent.action.SEND" />
    <data android:mimeType="*/*" />
  </intent>
</queries>

问题2

Error: Cannot run with sound null safety, because the following dependencies
don't support null safety:

解决办法

如果您使用的是Vscode

  • 第一步 File => Preferences => Settings

image.png

  • 第2步 Search for "Flutter run additional args"

image.png

  • 第3步 then click Add Item

now type --no-sound-null-safety 点击ok即可

image.png Cannot run with sound null safety because dependencies don't support null safety

问题3

Non-nullable instance field 'repoName' must be initialized.
Try adding an initializer expression, or add a field initializer in this constructor, or mark it 'late'.dartnot_initialized_non_nullable_instance_field

解决办法: 因flutter2.0添加了Sound null safety空安全声明,目的是通过显式声明可能为null的变量,增加Dart语言的鲁棒性。

因为Dart语言变量可以存null或者具体的值,因此在日常的开发中可能因为忘记赋值或者变量延迟赋值,导致访问某个变量时为null,导致程序运行时抛出exception。 这个功能推出后,可以从源码级解决null异常导致的错误。 简单的操作是在类型声明后添加?以标识这个变量是可以为null的。

class GitEvent {
  String? id; 
  String? userName; 
  String? avatarUrl; 
  String? repoName; 

  GitEvent(json) {

    this.id = json['id'];
    this.userName = json['actor']['login'];
    this.avatarUrl = json['actor']['avatar_url'];
    this.repoName = json['repo']['name'];

  }
}

问题3

The argument type 'String' can't be assigned to the parameter type 'Uri'

解决办法:

import 'package:http/http.dart' as http; 

var url = Uri.parse('https://example.com/whatsit/create'); 
var response = await http.post(url, body: {'name': 'doodle', 'color': 'blue'}); 
print('Response status: ${response.statusCode}'); 
print('Response body: ${response.body}'); 

print(await http.read('https://example.com/foobar.txt')); 

问题4


[dismissible] flutter pub get
Error detected in pubspec.yaml:
Error on line 31, column 10: Mapping values are not allowed here. Did you miss a colon earlier?
   ╷
31 │      http: ^0.13.3
   │          ^
   ╵
Please correct the pubspec.yaml file at C:\Users\Administrator\Desktop\flutter_example\dismissible\pubspec.yaml

解决办法 pubspec.yaml文件有严格的缩进,如果没有缩进没有严格按照规格来,就会报错,所以需要对齐改正即可。