flutter 学习 中遇到的问题

2021-10-23  本文已影响0人  小木虫666

目前使用的是vscode开发,mac电脑,之前是iOS开发

学习书籍《flutter实战》电子书

flutter 环境搭建网上有很多教程,自己搭建的时候碰到一些问题

虽然是个小问题,但是还是让自己困惑了一阵,记录一下!

B页面代码如下:( {'reload': true}就是传递给A页面的参数是个Map)

Navigator.pop(ctx.context, {'reload': true});

A页面pushB页面代码(报错的代码):

void _reloadData(Action action, Context<ProcurementState> ctx) async {
  Map info = action.payload;
  int id = info['id'];
  bool isEdit = info['isEdit'];
  int purchaseOrderId = info['purchaseOrderId'];
// 问题就出现在返回的result 使用了Map接收
  Map result = await Navigator.of(ctx.context)
      .pushNamed(RouteConfig.addGoodsPage, arguments: {
    "id": id,
    'isEdit': isEdit,
    'purchaseOrderId': purchaseOrderId
  });

  if (result is Map) {
    if (result['reload']) {
      fetchGoodsList(action, ctx, ctx.state.id);
    }
  }
}

其实报错信息里也提示了说类型不匹配,但是之前自己是做iOS开发的,我传递过去的是个Map,为啥不能用Map接收呢,这个与Dart语法有关,正确的代码是把Map换成var:

void _reloadData(Action action, Context<ProcurementState> ctx) async {
  Map info = action.payload;
  int id = info['id'];
  bool isEdit = info['isEdit'];
  int purchaseOrderId = info['purchaseOrderId'];
// 问题就出现在返回的result 使用了Map接收
  var result = await Navigator.of(ctx.context)
      .pushNamed(RouteConfig.addGoodsPage, arguments: {
    "id": id,
    'isEdit': isEdit,
    'purchaseOrderId': purchaseOrderId
  });

  if (result is Map) {
    if (result['reload']) {
      fetchGoodsList(action, ctx, ctx.state.id);
    }
  }
}

同样如果给B页面传参的时候直接把info塞到arguments里也是会报错:Exception has occurred.

_TypeError (type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>' of 'param')
也是个类型不匹配的问题,所以代码里才把info拆开

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<UserModel>(
      future: _checkLogin(context),
      builder: (_, snapshot) {
        if (snapshot.hasData && snapshot.data != null) {
          UserModel model = snapshot.data;
          if (model.nickName != null) {
            return createApp(RouteConfig.homePage, model.nickName);
          } else {
            return createApp(RouteConfig.loginPage, '');
          }
        } else {
          return Container(
            color: Colors.white,
            child: Image.asset('images/launch.png'),
          );
        }
      },
    );
  }

  Future<UserModel> _checkLogin(BuildContext context) async {
    UserModel model = await UserCenter().fetchUserModel();
    return model;
  }

void _updateGoodsListAction(params)(){}

仅仅是作为学习笔记,没有商业意图!

未完待续。。。

上一篇下一篇

猜你喜欢

热点阅读