Flutter 踩坑

2020-03-27  本文已影响0人  CN_HarrySun

1. 调用接口传入 List 类型不对导致无法获取到数据

初始化 List 没有指定类型,调用接口时直接传入导致无法获取数据(接口内部需要传入 List<int>)

原因:
没有指定类型的 List 是 List<dynamic>,不是 List<int> 类型的,所以接口无法拿到对应数据


解决:
将 List 定义为 List<int>

2. iOS 直接通过 Xcode 打包打开闪退问题

iOS 打包打开直接闪退问题

需要先进入 example 目录中,执行 `flutter build ios`,然后再使用 Xcode archive 就可以了。

3. SDK 发版后,平台带了 Web

需要在 SDKpubspec.yaml 文件中将下面代码添加进去用来指定平台

flutter:
  plugin:
    platforms:
      android:
        package: com.example.hello
        pluginClass: HelloPlugin
      ios:
        pluginClass: HelloPlugin

参考链接:指定版本

4. 切换 tab 后页面被重新加载


final List<StatefulWidget> vcList = [new ConversationListPage(),new ContactsPage()];

// 记录当前选中的 tab index
int curIndex = 0;

@override
  Widget build(BuildContext context) {
    return new Scaffold(
      bottomNavigationBar: new BottomNavigationBar(
        items: tabbarList,
        type: BottomNavigationBarType.fixed,
        onTap: (int index) {
          setState(() {
            curIndex = index;
          });
        },
        currentIndex: curIndex,
      ),
        body: vcList[curIndex],
    );
  }

body 修改为使用 IndexedStack

@override
  Widget build(BuildContext context) {
    return new Scaffold(
      bottomNavigationBar: new BottomNavigationBar(
        items: tabbarList,
        type: BottomNavigationBarType.fixed,
        onTap: (int index) {
          setState(() {
            curIndex = index;
          });
        },
        currentIndex: curIndex,
      ),
      // 这里的 body 修改为 IndexedStack 就可以保证切换时页面不会被重新加载
      body: IndexedStack(
        index: curIndex,
        children: <Widget>[new ConversationListPage(),new ContactsPage()],
      ),
    );
  }

5. 进入详情页面返回列表页,列表无法保持滚动状态,会返回到最上方

ScrollController _scrollController;
// 用于记录偏移量
double mPosition = 0;



// 添加滚动的监听
void _addScroolListener() {
    _scrollController.addListener(() {
      mPosition = _scrollController.position.pixels;
    });
  }



// 初始化时将 _scrollController 赋值给 ListView 的 controller
Widget _buildConversationListView() {
    return new ListView.builder(
      scrollDirection: Axis.vertical,
      itemCount: conList.length,
      // 初始化时将 _scrollController 赋值给 ListView 的 controller
      controller: _scrollController,
      itemBuilder: (BuildContext context, int index) {
        if (conList.length <= 0) {
          return WidgetUtil.buildEmptyWidget();
        }
        return ConversationListItem(
            delegate: this, conversation: conList[index]);
      },
    );
  }
  
  

// build 时让 scrollController 偏移到指定位置
@override
  Widget build(BuildContext context) {
    this._scrollController = ScrollController(initialScrollOffset: mPosition);
    _addScroolListener();
    return new Scaffold(
      appBar: AppBar(
        title: Text("IM Test"),
      ),
      key: UniqueKey(),
      body: _buildConversationListView(),
    );
  }

6. textField 设置 text 时,Android 光标在最前方,iOS 光标在最后方

将光标移动到最后方即可

void setText(String textContent) {
    if (textContent == null) {
      textContent = '';
    }
    this.textEditingController.text =
        this.textEditingController.text + textContent;
    // 将光标移动到最后方
    this.textEditingController.selection = TextSelection.fromPosition(
        TextPosition(offset: textEditingController.text.length));
    _refreshUI();
  }
上一篇下一篇

猜你喜欢

热点阅读