Flutter url_launcher 報錯 canLaunch will return false(Android)的解決辦法

fanyee^_^發表於2021-08-31

這是我參與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

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

解決辦法

If you are using VS Code

Then Goto

File => Preferences => Settings

Search for "Flutter run additional args"

then click Add Item

now type --no-sound-null-safety

click ok
複製程式碼

問題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檔案有嚴格的縮排,如果沒有縮排沒有嚴格按照規格來,就會報錯,所以需要對齊改正即可。

相關文章