Flutter升級至3.0.0版本問題記錄

1.Stack組件的 overflow 屬性被廢棄,需改為使用 clipBehavior 屬性

原有屬性報錯截圖:


image.png

解決方案:


image.png
overflow: Overflow.visible   
改為   
clipBehavior: Clip.none
2.原有flutter package里面的FlatButton組件被廢棄,需要用其他按鈕組件代替(這里我用自定義按鈕組件代替)

原有組件使用報錯截圖:


image.png

image.png

解決方案:


image.png
其中,initInkWellBtn的實現(xiàn)為:
/// 全局自定義按鈕 - 放在公共文件下,可供項目任意處調(diào)用
Widget initInkWellBtn(Widget widget, Function action) {
  return InkWell(
    onTap: () {
      debounce(action);
    },
    child: widget,
  );
}
3.Scaffold.of(context)showSnackBar屬性被廢棄

原有屬性報錯截圖:

image.png

解決方案:將Scaffold替換為ScaffoldMessenger,即:

image.png
4.WhitelistingTextInputFormatter屬性被廢棄

原有屬性報錯截圖:

image.png

解決方案:WhitelistingTextInputFormatter屬性替換為:FilteringTextInputFormatter

image.png
5.RaisedButton組件被廢棄,改為使用ElevatedButton

原有組件報錯截圖:


image.png

解決方案:


image.png
ElevatedButton(style: ButtonStyle(
  shape: MaterialStateProperty.all(RoundedRectangleBorder(
      borderRadius: BorderRadius.circular(this.borderRadius))),
  backgroundColor: MaterialStateProperty.all(
      isGradient ? Colors.transparent : color),
  // 設(shè)為透明色
  elevation: MaterialStateProperty.all(0),
 ))
6.flutter升級至3.0.0版本之后,運行flutter doctor正常,再運行flutter pub get報錯:

image.png

暫時刪除flutter_test組件,后續(xù)需要的時候再引入

7.運行成功之后,報警告:
image.png
Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null

指的是Flutter3.0.0之后,WidgetsBinding的實例instance不能為空,所以不需要!

解決方案:

1、本地的語法如果用到WidgetsBinding.instance,則手動去掉!
2、第三方庫需要去 pub.dev 檢查當(dāng)前版本是否適配Flutter 3.0.0,以下是本人整理的需要升級至匹配Flutter 3.0.0的庫:

`provider`的版本由`5.0.0`升級至`6.0.0`
`wechat_assets_picker`的版本由`5.5.8`升級至`6.0.0`
`percent_indicator`的版本由`3.0.1`升級至`4.2.0`
`qr_code_scanner`的版本由`0.6.1`升級至`1.0.0`
`get`的版本由`4.3.8`升級至`4.6.2`
8.下拉刷新pull_to_refresh組件已不支持Flutter 3.0.0,需替換為pull_to_refresh_flutter3 ^2.0.1,語法基本一致,手動修改下文件引用即可
image.png

解決方案:


image.png

image.png
9.dropdown_search的版本由1.0.0升級至4.0.0
(1)修改DropdownSearch類型為menu
image.png

解決方案如下:


image.png
/// 菜單選中配置
popupProps: PopupProps.menu(
  showSelectedItems: true,
  itemBuilder: customPopupItemBuilderExample,
),
/// hint文案
dropdownSearchDecoration: InputDecoration(
  hintText: '請選擇改價原因'
),
(2)修改DropdownSearch類型為bottomSheet

報錯如圖:


image.png

解決方案如圖:


image.png
popupProps: PopupProps.modalBottomSheet(
                showSelectedItems: true,
                itemBuilder: customPopupItemBuilderExample,
                modalBottomSheetProps: ModalBottomSheetProps(
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.only(
                            topLeft: Radius.circular(12),
                            topRight: Radius.circular(12))))),

(3)修改DropdownSearch類型為dialog
報錯如圖示:

image.png

解決方案如圖:


image.png
asyncItems: (filter) => logic
                                              .getBuyerList(filter),
                                        popupProps: PopupProps.dialog(
                                          showSearchBox: true,
                                          dialogProps: DialogProps(
                                            constraints: BoxConstraints(maxHeight: 300)
                                          )
                                        )
10.圖表charts_flutter組件已不支持Flutter 3.0.0,需替換為charts_flutter_new ^0.12.0,語法基本一致,手動修改下文件引用即可

報錯截圖如下:


image.png

解決方案修改:


image.png
import 'package:charts_flutter_new/flutter.dart' as charts;
11.圖片多選組件multi_image_picker已不支持Flutter 3.0.0,刪除,更換為wechat_assets_picker ^7.3.0及以上版本

報錯如下:


image.png

解決方案如下:


image.png
/// 必要的時候調(diào)用`clickIconFromPlatform `方法即可
CommonUtil.clickIconFromPlatform(5).then(data) {
  /// 選中的圖片數(shù)據(jù)data
 console.log(data);
}
static Future<List<MultipartFile>> clickIconFromPlatform(count) async {
    List<MultipartFile> files = [];
    try {
      List<AssetEntity>? result = await AssetPicker.pickAssets(getCurContext(),
          pickerConfig: AssetPickerConfig(
            maxAssets: count,
            requestType: RequestType.image,
            themeColor: ColorConfig.main
          )
          );
      List<AssetEntity> resultList = result ?? [];
      if (resultList.length > 0) {
        for (int i = 0; i < resultList.length; i++) {
          AssetEntity entity = resultList[i];
          Uint8List? imageData = await entity.thumbnailDataWithSize(ThumbnailSize((entity.width*0.7).toInt(), (entity.height*0.7).toInt()), quality: 70);
          if(imageData!=null){
            //獲得一個uuud碼用于給圖片命名
            final String uuid = Uuid().v1();
            //獲得應(yīng)用臨時目錄路徑
            final Directory _directory = await getTemporaryDirectory();
            final Directory _imageDirectory =
            await new Directory('${_directory.path}/image/')
                .create(recursive: true);
            var path = _imageDirectory.path;
            print('本次獲得路徑:${_imageDirectory.path}');
            //將壓縮的圖片暫時存入應(yīng)用緩存目錄
            File imageFile = new File('${path}originalImage_$uuid.png')
              ..writeAsBytesSync(imageData.toList());
            print(imageFile.path);

            var file = MultipartFile.fromFileSync(imageFile.path,
                filename: 'originalImage_${uuid}.png');
            files.add(file);
          }
        }
      }
      return files;
    } catch (e) {}
    return [];
  }
12.點擊某些頁面,報錯如下:
Unhandled Exception: This widget has been unmounted, so the State no longer has a context (and should be considered defunct).
Consider canceling any active work during "dispose" or using the "mounted" getter to determine if the State is still active

image.png

解決方案:
在當(dāng)前StatefulWidgetinitState的方法里完成初始化操作

@override
  void initState() {
    super.initState();
    /// 完成初始化操作
  }
13.A頁面跳轉(zhuǎn)B頁面(B頁面為預(yù)覽圖片頁),再從B頁面返回時,報錯如下:
[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: Looking up a deactivated widget's ancestor is unsafe.
At this point the state of the widget's element tree is no longer stable.
To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.

解決方案:上面說的意思是不是安全的引用小部件,就是已經(jīng)銷毀的界面然后重復(fù)銷毀,會報上面錯誤

方案1:
// 只需在報錯代碼行外層加個判斷,關(guān)鍵代碼如下所示:
// 當(dāng)mounted為true時才可以調(diào)用context的屬性或方法
if (context.mounted) {
   // 報錯的代碼
}

方案2:
// 項目中用到`photo_view `組件庫,當(dāng)前版本為`0.13.0`,升級為`0.14.0`,版本更新中有組件已解決
image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容