Flutter学习01--保持页面状态,模型转换报错记录

156 阅读1分钟

保持页面状态

tabBar切换,导致首页的initState频繁调用,为了解决这个问题,使用Flutter中Api,AutomaticKeepAliveClientMixin。保留tab的状态,避免initState方法重复调用。

class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin {

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}

在线json转模型

app.quicktype.io/

模型转换报错

主要报错原因:服务端返回的类型与客户端写的接收类型不一致导致,一般出现在double和int。

报错场景:例如购物车数量,模型转换报错type 'double' is not a subtype of type 'int'

报错原因:服务端返回类型double,使用int接收导致

解决方式:

第一种:改成double接收。

第二种:在map映射时候强转为int。
salesMinOrderQty: json["salesMinOrderQty"].toInt(),