flutter开发记录

309 阅读1分钟

一、provider

说明:常用ChangeNotifier管理状态,ChangeNotifierProvider提供状态(在页面根目录),当数据发生改变时,使用notifyListeners()通知页面更新,页面使用provider.of、consumer、selector方式获取状态。

1、问题记录

问题描述:使用ChangeNotifier和ChangeNotifierProvider,页面使用Provider.of(context)获取状态管理的数据,需要注意的是context要指向同一个,因为flutter层层套娃,有时候代码过多,就需要提取出一部分逻辑独立出来,这时候就容易出现嵌套在父页面的子页面的数据刷新了,父页面却没有刷新。

解决方法:1、子页面需要用到的数据通过父页面加载传递,子页面不做初始化工作,子页面不要ChangeNotifierProvider提供状态。2、页面不分离。

二、开发中碰到的难点

1、让某个节点在滚动可视区域时刻显示

首先要给滚动条命controller ScrollController scrollController = ScrollController();

其次需要给想要显示的节点命名key final key = GlobalKey();

滚动条滚动到指定地方的方法有两种:animateTo 和jumpTo,我这边使用的是animateTo

    // 跳到指定位置
    WidgetsBinding.instance.addPostFrameCallback((duration) {
      RenderBox box = key?.currentContext?.findRenderObject();
      // 判断该节点位置是否超过可视化区域
      if (MediaQuery.of(context).size.height <
          box?.localToGlobal(Offset?.zero)?.dy)
        // 滚动条跳到指定位置
        scrollController.animateTo(box?.localToGlobal(Offset?.zero)?.dy,
            duration: Duration(milliseconds: 200), curve: Curves.ease);
    });

WidgetsBinding.instance.addPostFrameCallback((duration) {})中运行是为了在页面加载完毕后执行,不然会报错。addPostFrameCallback方法是等页面加载完后回调里面的方法。

2、flutter iOS打包

使用 flutter build ios --flavor dev lib/main_dev.dart 对APP进行打包,报错

Xcode's output:
↳
    note: Using new build system
    note: Building targets in parallel
    note: Planning build
    note: Constructing build description
    error: No profiles for 'xxx' were found: Xcode couldn't find any iOS App Development provisioning profiles matching 'xxx'. Automatic
    signing is disabled and unable to generate a profile. To enable automatic signing, pass -allowProvisioningUpdates to xcodebuild. (in target 'Runner' from project
    'Runner')

发现原因:Xcode没有登录个人账号,没有加入公司的团队,项目中应该是使用到了公司的签名证书,导致编译失败。

打包.ipa报错cannot load such file — sqlite3 (LoadError)

/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- cfpropertylist (LoadError)
    from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Applications/Xcode.app/Contents/Developer/usr/bin/ipatool:15:in `<main>'
2020-02-05 07:57:11 +0000  /Applications/Xcode.app/Contents/Developer/usr/bin/ipatool exited with 1
2020-02-05 07:57:11 +0000  ipatool JSON: (null)

解决方法

$ gem install CFPropertyList
$ gem install sqlite3

并重启电脑。(方法转自 链接