Flutter走過的坑(持續更新)

1 Target of URI doesn't exist 'package:flutter/material.dart'html

官方下載的flutter中有一個example文件夾,裏面有不少flutter的小demo,打開其中一個demo,執行運行的指令後,出現該錯誤。ios

錯誤緣由是沒有下載flutter依賴包,就像RN沒有執行npm install 同樣npm

解決方法:執行flutter packages getxcode

2 flutter: command not found服務器

flutter環境變量的配置網絡

mac配置環境變量參考該網址https://jingyan.baidu.com/article/8065f87f47b29523312498e4.htmlapp

export PUB_HOSTED_URL=https://pub.flutter-io.cnless

export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cnide

export PATH="$PWD/flutter/bin:$PATH"post

關閉終端,從新打開一次就能夠了

具體能夠參考https://flutter.dev/community/china

3 Error on line 45, column 4 of pubspec.yaml: Expected a key while parsing a block mapping

assets:
^

assets前面多了一個空格,刪除就行了

4 More than one device connected; please specify a device with the '-d <deviceId>' flag, or use '-d all' to act on all devices.

ios和flutter混合開發,使用flutter attach能夠調試flutter代碼

可是運行flutter attach後並無出現預期的效果,而是出現了

More than one device connected; please specify a device with the '-d <deviceId>' flag, or use '-d all' to act on all devices.

 

「xxx」的 iPhone • b13a6cf6ca28106dcbd0d6273c205d0fec8583aa • ios • iOS 12.1.2

iPhone X      • 108E3AF0-CFF1-4069-8989-0CF95B2EAD31     • ios • iOS 12.1 (simulator)

當時就仔細看第一行是啥意思,又上網查資料浪費的好長時間,感受是個好低級的錯誤。

其實就是同時鏈接了2個設備,運行flutter attach -d <deviceId>選擇其中一個就能夠了,

或者斷開一個設備,只鏈接一個設備就行了

 

5 A RenderFlex overflowed by 240 pixels on the right

渲染的部分超出屏幕右側的寬度了

解決外層包裹一個能夠滑動的widget,例如SingleChildScrollView,

由於我是水平方向超出了,因此還須要加上

scrollDirection: Axis.horizontal,
 
6 ListView運行時, 報錯Vertical viewport was given unbounded height
解決
能夠經過指定 shrinkWrap = true爲了你 ListView
ListView(
  shrinkWrap: true,
  children...
)

可參考: https://cloud.tencent.com/developer/ask/138715

 

7 ScrollView嵌套ListView滾動衝突

解決: ListView添加physics:NeverScrollableScrollPhysics(),禁用ListView的滾動。

 
8 使用Android Studio調試項目
咱們點擊AS狀態欄的flutter attach能夠調試咱們的項目(打斷點,看變量等),當進入flutter頁面時,須要等一段時間才能鏈接上個人的手機或模擬器,而網絡請求部分在等待時間就請求完了,沒有進入斷點。
解決:在網絡請求前加一個延時(
await Future.delayed(Duration(seconds: 6,));
),這樣能夠保證鏈接成功前尚未運行到咱們的斷電,成功後正常進入斷點。
 
9 The element type 'Type' can't be assigned to the list type 'Widget'
body: TabBarView(
        controller: model._tabController,
        children: <Widget>[
          Content,
          Content
        ]
      )


class Content extends StatelessWidget{

  @override
  Widget build(BuildContext context) {
    return Center(child: Text("測試"),);
  }

}

解決: 調用時加上小括號,將Content改爲Content()就能夠了

 10 dart中map方法獲取index

參考: https://cloud.tencent.com/developer/ask/207919/answer/321886

List arr = ["a", "b", "c"];
List.asMap().map((index, item)=> MapEntry(index,Text(item)
)).values.toList()

11 當電腦刪除原來的flutter sdk,而後在官網上下載最新的flutter版本後,建議從新更新環境變量,不然會有問題

 

12. 關於GestureDetector中onTap點擊事件對空白處點擊不相應的bug

GestureDetector(
  child: Container(
    child: Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: <Widget>[
        Text("左"),
        Text("右")
      ],
    ),
  ),
  onTap: () {
    print("點擊了Container");
  },
);

如上代碼所示,當點擊文字「左」「右」時會觸發點擊時間,點擊其餘地方並不會觸發點擊事件,若是想點擊其餘空白處也觸發點擊事件,能夠在Contaier加一個color屬性,以下代碼所示 

GestureDetector(
  child: Container(
    color: Colors.transparent,
    child: Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: <Widget>[
        Text("左"),
        Text("右")
      ],
    ),
  ),
  onTap: () {
    print("點擊了Container");
  },
);

 13 因爲本地flutter環境與服務器打包flutter環境不一致致使連續點擊2次flutter頁面崩潰的問題

原來使用的flutter版本爲1.2.1,以後升級了flutter版本到1.5.4,對應pubspec.lock中對應的依賴版本有變化

只是本地升級,服務器環境沒有升級,因此打包時環境不對,出現了連續點擊2次flutter頁面致使崩潰,

升級服務器的flutter環境後,問題解決

 

14 pageView放在stack裏,而此時stack是column的子組件,容易出錯,將放置順序調整爲column放在stack裏面,pageView放在column中沒有問題

須要主要stack和colum的放置順序

 

15  Unhandled Exception: 'package:flutter/src/widgets/scroll_controller.dart': Failed assertion: line 110 pos 12: '_positions.isNotEmpty': ScrollController not attached to any scroll views.

這個問題找解決方案找了很長時間

終於在這個網站https://stackoverflow.com/questions/52296387/scrollcontroller-jumpto-scrollcontroller-not-attached-to-any-scroll-views看到解決的要點

問題是出在

_pageController.animateToPage(
        _index, 
        duration: changeTimeOut, 
        curve: Curves.linear
      );

 我使用了上面的代碼,可是此時_pageController是沒有的(向上滑動後這部分UI不顯示了,因此flutter就不渲染了,我理解_pageController也就不存在了),因此報錯了。

解決方案是加一個判斷,等向上滑動後UI不顯示了,就不執行這部分代碼就行了

上面那個網站給出了是 if (_scrollController.hasClients){}

對應個人代碼的解決方案是if (_scrollController.offset < 200) {上面的代碼}

16 輸入flutter attach時出現如下提示 

Waiting for another flutter command to release the startup lock...

解決:

此時須要打開 flutter/bin/cache/lockfile,刪除就好了

參考:https://www.jianshu.com/p/7507d087e9f1

 

17 更新flutter版本1.7.8+hotfix.3後出現如下錯誤

Error: The argument type 'Null Function(ImageInfo, bool)' can't be assigned to the parameter type 'ImageStreamListener'.

 

解決方法: 

將出現錯誤的地方

_imageStream.addListener(_handleImageLoaded);

改爲

_imageStream.addListener(ImageStreamListener(_handleImageLoaded));

 

18 [FATAL:flutter/third_party/txt/src/minikin/FontCollection.cpp(95)] nTypefaces == 0 

[ERROR:flutter/third_party/txt/src/minikin/FontFamily.cpp(184)] Could not get cmap table size!

緣由是引用的字體或者圖標不存在

 

19 [FATAL:flutter/runtime/dart_vm.cc(380)] Error while initializing the Dart VM: Wrong full snapshot version, expected '1d7acad1540192ac459cf60344efb7c1' found 'c8562f0ee0ebc38ba217c7955956d1cb'

問題發生於升級flutter sdk以後,看上邊的日誌Wrong full snapshot version能夠猜想:升級了sdk,可是以前已生成的編譯產物仍是舊的,不匹配,須要從新build一下

1) removing the content of flutter/bin/cache
2)  thenrunning flutter upgrade again
3) thenrunning flutter clean prior to flutter run
參考 https://juejin.im/post/5d1c596d6fb9a07ee742fa59
 
20 flutter升級到1.9.1 後 ios打包失敗 Flutter.framework: Permission denied
 
這是flutter官方的一個bug,查資料說flutter升級到1.10就行了
不升級flutter的狀況下,能夠改Flutter SDK 的一個文件,flutter/packages/flutter_tools/bin/xcode_backend.sh
144行

RunCommand find "${derived_dir}/engine/Flutter.framework" -type f -exec chmod a-w "{}" \;

=>

RunCommand find "${derived_dir}/engine/Flutter.framework" -type f -iname '.h' -exec chmod a-w "{}" \;
相關文章
相關標籤/搜索