您好,欢迎来到二三娱乐。
搜索
您的当前位置:首页Flutter实战之WanAndroid项目中碰到的问题

Flutter实战之WanAndroid项目中碰到的问题

来源:二三娱乐

写在前面

项目地址

WanAndroid项目Release版运行效果

开发环境搭建及模拟器相关

PANIC: Missing emulator engine program for 'x86' CPU.

在启动模拟器运行Flutter时可能出现如上错误,原因是 Sdk(AndroidSDK安装位置 )/ tools下文件丢失,删除重新安装就好了。步骤如下图:

取消2中Andrid SDK Tools的勾选,然后点击Apply开始卸载,卸载完成之后,勾选Andrid SDK Tools,然后点击Apply开始安装。完成之后重启AndroidStudio(记得重启)

操作图

Error while initializing the Dart VM

Windows开发环境下,Android在打包之后成功之后,安装release版apk时闪退,并且出现错误日志

Error while initializing the Dart VM

WanAndroid项目相关

首页直接在MaterialApp中直接使用Navigator.push

class WanAndroidApp extends StatefulWidget {
  @override
  _WanAndroidState createState() => new _WanAndroidState();
}
class _WanAndroidState extends State<WanAndroidApp>
    with TickerProviderStateMixin {

  @override
  Widget build(BuildContext context) {
    initData();
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text('标题'),
          actions: <Widget>[
            new IconButton(
                icon: new Icon(Icons.search),
                onPressed: () {
Navigator.push(context,new MaterialPageRoute(
                     builder: (context) => new SearchPage()));
                })
          ],
        ),
        body: ...
    );
  }
}

直接点击是搜索按钮,会报错,具体信息如下,为什么呢?

The following assertion was thrown while handling a gesture: Navigator operation requested with a context that does not include a Navigator. 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.

Another exception was thrown: Navigator operation requested with a context that does not include a Navigator.

stackoverflow上有人给出了答案,如下

Think of the widgets in Flutter as a tree, with the context pointing to whichever node is being built with the build function. In your case, you have
--------------------------------------------------------------
MainScreen    <------ context
  --> MaterialApp
   (--> Navigator built within MaterialApp)
      --> Scaffold
        --> App Bar
          --> ...
        --> Center
          --> ...
--------------------------------------------------------------
So when you're using the context to find the Navigator, you're using a context for the MainScreen which isn't under the navigator.

意思就是此时Navigator.push(context..中的contexts是MainScreen中的context

RefreshIndicator在ListView条目较少时不触发下拉刷新

RefreshIndicator是根据下拉时的偏移量触发刷新,当条目较少时(未占满一个屏幕),ListView不能滚动,所以无法触发下拉刷新,给ListView的physice属性设置值为new AlwaysScrollableScrollPhysics(),让ListView在任何情况下都可以滑动,也就可以触发RefreshIndicator的刷新。

Widget listView = new ListView.builder(
  //注意这里physics
  physics: new AlwaysScrollableScrollPhysics(),
  itemCount: listData.length,
  itemBuilder: (context, i) => buildItem(i),
  controller: _contraller,
);

TarBarView每次切换时其条目Widget都会执行initState()

class ArticleListPageState extends State<ArticleListPage>
    with AutomaticKeepAliveClientMixin {

  //  with AutomaticKeepAliveClientMixin 并且get wantKeepAlive返回true,tab切换时,不会每次执行initState
  //来自 
  @override
  bool get wantKeepAlive => true;
}

后续会再慢慢完善这篇博客

Copyright © 2019- yule263.com 版权所有 湘ICP备2023023988号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务