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

Flutter編譯問題

1.Flutter編譯時一直卡在"Resolving dependencies"

原因:國內被牆的原因

解決方案1:
配置系統環境變量:

解決方案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:
使用翻牆工具構建

2.運行一些開源的項目編輯報錯提示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開發問題

1.使用Navigator.push()跳轉界面報錯:

'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不支持堆棧管理,比如一個類直接繼承自StatelessWidgetStatefulWidget,而它本身或父類沒有堆棧管理功能。那麼這個類是不支持頁面跳轉的,所以就會報錯。要查看是否支持頁面跳轉,直接進入源碼,比如MaterialApp類,它是支持的,那麼它的源碼裏會有對應的功能介紹:

/// 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.  //這裏做了對應的介紹
///  * [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 {
  .....
}

放一個報錯的代碼:

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("我是從上個頁面傳遞回來的值$value");
                });
              }, child:new Icon(Icons.build)),
            )
        )
    );
  }
}

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

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("我是從上個頁面傳遞回來的值$value");
            });
          },
          child: new Icon(Icons.build)),
    ));
  }
}

代碼中將MaterialApp移出來了,直接將MyApp包裹了,那麼MyAppcontext就生效了,代碼也不會報錯啦。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章