美文网首页
Flutter入门笔记二

Flutter入门笔记二

作者: brave_wu | 来源:发表于2023-01-12 14:20 被阅读0次

    搭建开发环境

    学完dart语言的基础,接着就是开发环境啦:
    1.下载SDK
    2.下载好了解压到xxx目录下
    3.配置环境变量

    $vim ~/.bash_profile
    

    添加下面几行

    export PUB_HOSTED_URL=https://pub.flutter-io.cn
    export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
    export PATH=xxx/flutter/bin:$PATH
    export PATH=${PATH}:xxx/flutter/bin/cache/dart-sdk
    

    更新一下配置

    source ~/.bash_profile
    

    4.经过上面几步,flutter已经在mac安家了,flutter相关命令也可以使用了,如

    flutter doctor
    flutter -h (查看flutter命令的一下帮助)
    flutter --version(注意是 --version 查看flutter版本)
    flutter upgrade (flutter升级)
    

    5.编译器
    作为iOS开发者,xcode就无需担心了,需要安装Android Studio、VS Code、Chrome以方便写代码和调试、打包。
    开发我推荐VSCode,开始开发之前在插件市场安装好flutter同名插件。

    6.启动与调试
    6.1 新建一个flutter项目:VSCode中cmd+shift+p选择Flutter: New Application Project即可新建一个flutter应用。
    6.2 作为iOScoder顺序第一的肯定先启动iOS端,vc底部选择你连接好的iPhone,选择运行->启动调试(在ios项目中配置配置好了bundle ID和证书),很顺利的手机上已经打开了demo


    D23C8728D6358ACB7D947511722D5E95.png

    6.3 运行安卓程序
    android studio运行项目会进行下载相关支持,然后卡在kotlin-compiler-embeddable-1.6.10.jar上,这里可以使用阿里的镜像,具体的,在目录

    /Users/xx用户/.gradle
    

    中新建一个init.gradle文件,里面内容

    allprojects{
        repositories {
            def ALIYUN_REPOSITORY_URL = 'https://maven.aliyun.com/repository/central/'
            def ALIYUN_JCENTER_URL = 'https://maven.aliyun.com/repository/public/'
            all { ArtifactRepository repo ->
                if(repo instanceof MavenArtifactRepository){
                    def url = repo.url.toString()
                    if (url.startsWith('https://repo1.maven.org/maven2') || url.startsWith('http://repo1.maven.org/maven2')) {
                        project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL."
                        remove repo
                    }
                    if (url.startsWith('https://jcenter.bintray.com/') || url.startsWith('http://jcenter.bintray.com/')) {
                        project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER_URL."
                        remove repo
                    }
                }
            }
            maven {
                url ALIYUN_REPOSITORY_URL
                url ALIYUN_JCENTER_URL
                url 'https://maven.aliyun.com/repository/google/'
                url 'https://maven.aliyun.com/repository/gradle-plugin/'
            }
        }
     
     
        buildscript{
            repositories {
                def ALIYUN_REPOSITORY_URL = 'https://maven.aliyun.com/repository/central/'
                def ALIYUN_JCENTER_URL = 'https://maven.aliyun.com/repository/public/'
                all { ArtifactRepository repo ->
                    if(repo instanceof MavenArtifactRepository){
                        def url = repo.url.toString()
                        if (url.startsWith('https://repo1.maven.org/maven2') || url.startsWith('http://repo1.maven.org/maven2')) {
                            project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL."
                            remove repo
                        }
                        if (url.startsWith('https://jcenter.bintray.com/') || url.startsWith('http://jcenter.bintray.com/')) {
                            project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER_URL."
                            remove repo
                        }
                    }
                }
                maven {
                    url ALIYUN_REPOSITORY_URL
                    url ALIYUN_JCENTER_URL
                    url 'https://maven.aliyun.com/repository/google/'
                    url 'https://maven.aliyun.com/repository/gradle-plugin/'
                }
            }
        }
    }
    

    等下载完成再运行又出现

    FAILURE: Build failed with an exception. * What went wrong: A problem was found with the configuration of task ':app:processDebugResources' (type 'LinkApplicationAndroidResourcesTask'). - In plugin 'com.android.internal.version-check' type 'com.android.build.gradle.internal.res.LinkApplicationAndroidResourcesTask' property 'androidJarInput.androidJar' specifies file '/Users/xxx/Library/Android/sdk/platforms/android-31/android.jar' which doesn't exist.
    

    打开as的SDK Manager,选择安装andriud-31。
    再次执行flutter run又出现adb: failed to install apk,原因是安卓模拟器安装了其他APP,内存不够,我卸载了再运行就好了。

    6.4 运行在Chrome上,很顺利

    Launching lib/main.dart on Chrome in debug mode...
    
    lib/main.dart:1
    
    This app is linked to the debug service: ws://127.0.0.1:51257/utBPASg7syU=/ws
    
    Debug service listening on ws://127.0.0.1:51257/utBPASg7syU=/ws
    
    💪 Running with sound null safety 💪
    
    Connecting to VM Service at ws://127.0.0.1:51257/utBPASg7syU=/ws
    
    Flutter Web Bootstrap: Programmatic
    
    

    接下来就可以真正进行服啦特实战了!本文over!

    相关文章

      网友评论

          本文标题:Flutter入门笔记二

          本文链接:https://www.haomeiwen.com/subject/grpumktx.html