Flutter踩坑記錄(持續(xù)更新中)

Flutter編譯問題

1.Flutter編譯時(shí)一直卡在"Resolving dependencies"

原因:國內(nèi)被墻的原因

解決方案1:
配置系統(tǒng)環(huán)境變量:

  • PUB_HOSTED_URL->https://pub.flutter-io.cn
  • FLUTTER_STORAGE_BASE_URL->https://storage.flutter-io.cn
    最新的變量地址請(qǐng)?jiān)L問:https://flutter.io/community/china

解決方案2:
android目錄的build.gradle下添加阿里鏡像代理:

buildscript {
    repositories {
        maven { url 'https://maven.aliyun.com/repository/google' }
        maven { url 'https://maven.aliyun.com/repository/jcenter' }
        maven { url 'https://maven.aliyun.com/repository/public' }
        maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
    }
}

allprojects {
    repositories {
        maven { url 'https://maven.aliyun.com/repository/google' }
        maven { url 'https://maven.aliyun.com/repository/jcenter' }
        maven { url 'https://maven.aliyun.com/repository/public' }
        maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
        google()
        jcenter()
    }
}

解決方案3:
使用翻墻工具構(gòu)建

2.運(yùn)行一些開源的項(xiàng)目編輯報(bào)錯(cuò)提示Finished with error: FormatException: Bad UTF-8 encoding 0xc3 (at offset 169)

原因:app的release版本找不到keystore文件
解決方案:
找到android->app目錄下的build.gradle,找到buildTypes,修改releasesigningConfigs.releasedebug:

    buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }
        release {
            signingConfig signingConfigs.debug
        }
    }

或者自己重新配置release的簽名

Flutter開發(fā)問題

1.使用Navigator.push()跳轉(zhuǎn)界面報(bào)錯(cuò):

'Navigator operation requested with a context that does not include a Navigator.\n'
'The context used to push or pop routes from the Navigator must be that of a '
'widget that is a descendant of a Navigator widget.'

原因:傳入的context不支持堆棧管理,比如一個(gè)類直接繼承自StatelessWidgetStatefulWidget,而它本身或父類沒有堆棧管理功能。那么這個(gè)類是不支持頁面跳轉(zhuǎn)的,所以就會(huì)報(bào)錯(cuò)。要查看是否支持頁面跳轉(zhuǎn),直接進(jìn)入源碼,比如MaterialApp類,它是支持的,那么它的源碼里會(huì)有對(duì)應(yīng)的功能介紹:

/// See also:
///
///  * [Scaffold], which provides standard app elements like an [AppBar] and a [Drawer].
///  * [Navigator], which is used to manage the app's stack of pages.  //這里做了對(duì)應(yīng)的介紹
///  * [MaterialPageRoute], which defines an app page that transitions in a material-specific way.
///  * [WidgetsApp], which defines the basic app elements but does not depend on the material library.
///  * The Flutter Internationalization Tutorial,
///    <https://flutter.io/tutorials/internationalization/>.
class MaterialApp extends StatefulWidget {
  .....
}

放一個(gè)報(bào)錯(cuò)的代碼:

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        title: 'Welcome to Flutter',
        home: new Scaffold(
            body:Container(
              child: FlatButton(onPressed:(){
                var push = Navigator.push(context,MaterialPageRoute(builder: (BuildContext context) {
                  return new OtherPager("我是從main傳過去的值");
                }));
                push.then((value){
                print("我是從上個(gè)頁面?zhèn)鬟f回來的值$value");
                });
              }, child:new Icon(Icons.build)),
            )
        )
    );
  }
}

這里的MyApp是直接繼承自StatelessWidget的,而在runApp()中直接使用了它,它沒有父類而它本身也不支持界面的跳轉(zhuǎn),所以用它的context傳入到Navigator中肯定是會(huì)報(bào)錯(cuò)的
解決方案
使用一個(gè)有堆棧管理功能的父類將MyApp類包裹,使其具有堆棧跳轉(zhuǎn)功能
見如下代碼:

void main() => runApp(MaterialApp(home: MyApp()));

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
        body: Container(
      child: FlatButton(
          onPressed: () {
            var push = Navigator.push(context,
                MaterialPageRoute(builder: (BuildContext context) {
              return new OtherPager("我是從main傳過去的值");
            }));
            push.then((value) {
              print("我是從上個(gè)頁面?zhèn)鬟f回來的值$value");
            });
          },
          child: new Icon(Icons.build)),
    ));
  }
}

代碼中將MaterialApp移出來了,直接將MyApp包裹了,那么MyAppcontext就生效了,代碼也不會(huì)報(bào)錯(cuò)啦。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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