flutter2.1升级flutter3.0

  1. 下载最新fluttert版本 for github

  2. 升级kotlin版本 及 gradle版本

Module was compiled with an
incompatible version of Kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.15.
Failed to apply plugin ‘kotlin-android’.
[ +4 ms] > The current Gradle version 6.7 is not compatible with the Kotlin Gradle plugin. Please use Gradle 6.7.1 or newer, or the previous version of the Kotlin plugin

更改build.gradle

buildscript {
		ext.kotlin_version = '1.7.10' //改为更新版本
		...
	repositories {
        maven{ url 'https://maven.aliyun.com/nexus/content/groups/public/'}  //改为https
        ...
dependencies {
        classpath 'com.android.tools.build:gradle:7.2.0'  //改为更新版本
		...

更改gradle-wrapper.properties

distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip  //改为更新版本
  1. 升级gradle后,需要更改maven下载源

Using insecure protocols with repositories, without explicit opt-in, is unsupported. Switch Maven repository ‘maven(http://maven.aliyun.com/nexus/content/groups/public/)’ to redirect to asecure protocol (like HTTPS) or allow insecure protocols. See
https://docs.gradle.org/7.3.3/dsl/org.gradle.api.artifacts.repositories.UrlArtifactRepository.html#org.gradle.api.artifacts.repositories.UrlArtifactRepository:allowInsecureProtocol for more details.

Could not resolve all dependencies for configuration ‘:app:debugRuntimeClasspath’
[ ] > Using insecure protocols with repositories, without explicit opt-in, is unsupported. Switch Maven repository ‘maven(http://maven.aliyun.com/nexus/content/groups/public)’ to redirect to a secure
protocol (like HTTPS) or allow insecure protocols. See

Could not resolve all dependencies for configuration ‘:adv_camera:androidApis’.
[ ] > Using insecure protocols with repositories, without explicit opt-in, is unsupported. Switch Maven repository ‘maven(http://maven.aliyun.com/nexus/content/groups/public)’ to redirect to a secure
protocol (like HTTPS) or allow insecure protocols. See
https://docs.gradle.org/7.3.3/dsl/org.gradle.api.artifacts.repositories.UrlArtifactRepository.html#org.gradle.api.artifacts.repositories.UrlArtifactRepository:allowInsecureProtocol for more details.

$ vi ${flutterRoot}\packages\flutter_tools\gradle\flutter.gradle
maven(http://maven.aliyun.com/nexus/content/groups/public/)
改为
maven(https://maven.aliyun.com/nexus/content/groups/public/)
  1. 修改pubspec.yml sdk版本
    支持null safe的flutter最低版本是2.0.0,对应的dart sdk是2.12.0
environment:
  sdk: ">=2.12.0 <3.0.0" #改为2.12
  1. Gradle 7以后的版本是不可以用compile, 性能不如implementation

problem occurred evaluating project ‘:bluetooth_print’.
[ +1 ms] > Could not find method compile() for arguments [file collection] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

Could not run phased build action using connection to Gradle distribution ‘https://services.gradle.org/distributions/gradle-7.1.1-bin.zip’.
java.io.StreamCorruptedException: invalid type code: 00
invalid type code: 00Java(0)

尝试启动工程后,提示还有一些代码需要更改,一些包的升级以及业务代码中空安全的处理

  1. 一些小部件升级
  2. Scaffold.of(context).showSnackBar(…)
Try correcting the name to the name of an existing method, or defining a method named 'showSnackBar'.
[        ] [        ]                         Scaffold.of(context).showSnackBar(
[        ] [        ]                                              ^^^^^^^^^^^^

改为
ScaffoldMessenger.of(context).showSnackBar(…)

  1. FlatButton 改为 TextButton
    参照 https://github.com/flutter/plugins/commit/a7dd76950e5b240d2d878504a6f010ef24717e34
Try correcting the name to the name of an existing method, or defining a method named 'FlatButton'.
[        ] [        ]                 FlatButton(
[        ] [        ]                 ^^^^^^^^^^

  1. RaisedButton 改为 ElevatedButton
Try correcting the name to the name of an existing method, or defining a method named 'RaisedButton'.
[        ] [        ]             child: RaisedButton(
[        ] [        ]                    ^^^^^^^^^^^^

  1. WillPopScope#onWillPop返回true即退出
WillPopScope(
child: ... ,
onWillPop: () async {
  // 点击返回键的操作
  if(DateTime.now().difference(lastPopTime) > Duration(seconds: 1)){
	lastPopTime = DateTime.now();
	_gobackWebView();
  }else{
	lastPopTime = DateTime.now();
	// 退出app
	await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
  }
  return true;
}

改为


WillPopScope(
child: ... ,
onWillPop: () async {
     // 点击返回键的操作
     if(DateTime.now().difference(lastPopTime) > Duration(seconds: 1)){
       lastPopTime = DateTime.now();
       _gobackWebView();
       return false;
     }else{
       lastPopTime = DateTime.now();
       return true;// 退出app
     }
   }
  1. AsyncSnapshot获取data
    AsyncSnapshot snapshot
final WebViewController controller = snapshot.requireData;

改为

final WebViewController controller = snapshot.data!;
  1. 更新permission_handler到最新版本,需要用新的方式获取权限
lib/sample.dart:402:11: Error: 'PermissionGroup' isn't a type.
      Map permissions = await PermissionHandle
  1. 启动报如下错误
    A splash screen was provided to Flutter, but this is deprecated. See flutter.dev/go/android-splash-migration for migration steps
 注释掉
  1. 加了allowInsecureProtocol=true, 导致低版本的gradle报错
Launching lib\main.dart on AQM AL00 in debug mode...
Project evaluation failed including an error in afterEvaluate {}. Run with --stacktrace for details of the afterEvaluate {} error.

FAILURE: Build failed with an exception.

* Where:
Script '${flutterRoot}\packages\flutter_tools\gradle\flutter.gradle' line: 187

* What went wrong:
A problem occurred evaluating script.
> Failed to apply plugin [class 'FlutterPlugin']
   > Could not set unknown property 'allowInsecureProtocol' for object of type org.gradle.api.internal.artifacts.repositories.DefaultMavenArtifactRepository.
$ vi ${flutterRoot}\packages\flutter_tools\gradle\flutter.gradle 
删除 allowInsecureProtocol=true
  1. 打开相机报错 Unexpected platform view context. When constructing a platform view in the factory, use the context from PlatformViewFactory#create, view id: 1

I/PlatformViewsController(24384): Hosting view in a virtual display for platform view: 1
E/flutter (24384): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(error, java.lang.IllegalStateException: Unexpected platform view context. When constructing a platform view in the factory, use the context from PlatformViewFactory#create, view id: 1
E/flutter (24384): at io.flutter.plugin.platform.SingleViewPresentation.onCreate(SingleViewPresentation.java:187)
E/flutter (24384): at android.app.Dialog.dispatchOnCreate(Dialog.java:874)
E/flutter (24384): at android.app.Dialog.show(Dialog.java:521)
E/flutter (24384): at android.app.Presentation.show(Presentation.java:257)
E/flutter (24384): at io.flutter.plugin.platform.VirtualDisplayController.(VirtualDisplayController.java:119)
E/flutter (24384): at io.flutter.plugin.platform.VirtualDisplayController.create(VirtualDisplayController.java:64)
E/flutter (24384): at io.flutter.plugin.platform.PlatformViewsController$1.createForTextureLayer(PlatformViewsController.java:267)
E/flutter (24384): at io.flutter.embedding.engine.systemchannels.PlatformViewsChannel$1.create(PlatformViewsChannel.java:122)
E/flutter (24384): at io.flutter.embedding.engine.systemchannels.PlatformViewsChannel 1. o n M e t h o d C a l l ( P l a t f o r m V i e w s C h a n n e l . j a v a : 60 ) E / f l u t t e r ( 24384 ) : a t i o . f l u t t e r . p l u g i n . c o m m o n . M e t h o d C h a n n e l 1.onMethodCall(PlatformViewsChannel.java:60) E/flutter (24384): at io.flutter.plugin.common.MethodChannel 1.onMethodCall(PlatformViewsChannel.java:60)E/flutter(24384):atio.flutter.plugin.common.MethodChannelIncomingMethodCallHandler.onMessage(MethodChannel.java:261)
E/flutter (24384): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
E/flutter (24384): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue 0 0 0io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:321)
E/flutter (24384): at io.flutter.embedding.engine.dart.DartMessenger$ E x t e r n a l S y n t h e t i c L a m b d a 0. r u n ( U n k n o w n S o u r c e : 12 ) E / f l u t t e r ( 24384 ) : a t a n d r o i d . o s . H a n d l e r . h a n d l e C a l l b a c k ( H a n d l e r . j a v a : 900 ) E / f l u t t e r ( 24384 ) : a t a n d r o i d . o s . H a n d l e r . d i s p a t c h M e s s a g e ( H a n d l e r . j a v a : 103 ) E / f l u t t e r ( 24384 ) : a t a n d r o i d . o s . L o o p e r . l o o p ( L o o p e r . j a v a : 219 ) E / f l u t t e r ( 24384 ) : a t a n d r o i d . a p p . A c t i v i t y T h r e a d . m a i n ( A c t i v i t y T h r e a d . j a v a : 8668 ) E / f l u t t e r ( 24384 ) : a t j a v a . l a n g . r e f l e c t . M e t h o d . i n v o k e ( N a t i v e M e t h o d ) E / f l u t t e r ( 24384 ) : a t c o m . a n d r o i d . i n t e r n a l . o s . R u n t i m e I n i t ExternalSyntheticLambda0.run(Unknown Source:12) E/flutter (24384): at android.os.Handler.handleCallback(Handler.java:900) E/flutter (24384): at android.os.Handler.dispatchMessage(Handler.java:103) E/flutter (24384): at android.os.Looper.loop(Looper.java:219) E/flutter (24384): at android.app.ActivityThread.main(ActivityThread.java:8668) E/flutter (24384): at java.lang.reflect.Method.invoke(Native Method) E/flutter (24384): at com.android.internal.os.RuntimeInit ExternalSyntheticLambda0.run(UnknownSource:12)E/flutter(24384):atandroid.os.Handler.handleCallback(Handler.java:900)E/flutter(24384):atandroid.os.Handler.dispatchMessage(Handler.java:103)E/flutter(24384):atandroid.os.Looper.loop(Looper.java:219)E/flutter(24384):atandroid.app.ActivityThread.main(ActivityThread.java:8668)E/flutter(24384):atjava.lang.reflect.Method.invoke(NativeMethod)E/flutter(24384):atcom.android.internal.os.RuntimeInitMethodAndArgsCaller.run(RuntimeInit.java:513)
E/flutter (24384): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1109)

改为官方camera,还有一些包的更新,如下

pubspec.yml
	device_info: ^2.0.3
	url_launcher: ^6.1.5 
	path_provider: ^2.0.11
	permission_handler: ^10.0.0
	shared_preferences: ^2.0.15
	camera: ^0.10.0+1
	#相册
	barcode_scan2: ^4.0.1

编译时提示如下,修改为gradle对应版本

┌─ Flutter Fix ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ 
│ [!] Your project requires a newer version of the Kotlin Gradle plugin.                                                                                │ 
│ Find the latest version on https://kotlinlang.org/docs/gradle.html#plugin-and-versions, then update E:\code\flutter\webview_app\android\build.gradle: │ 
│ ext.kotlin_version = ''                                                                                                               │ 
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ 
build.gradle

buildscript {
    ext.kotlin_version = '1.7.10'
    dependencies {
        classpath 'com.android.tools.build:gradle:7.2.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

你可能感兴趣的:(flutter,android,gradle,flutter)