You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter滑动Google地图出现ParentDataElement断言错误如何解决?

Flutter Google地图滑动底部区域闪屏及断言错误修复方案

问题表现

移动Google地图时,更新后的位置信息可正常展示在底部容器中,但向上滑动位置信息区域时页面出现闪屏,同时抛出如下断言错误:
Failed assertion: line 4973 pos 16: 'child is! ParentDataElement<ParentData>': is not true

错误原因

  1. 在build方法中直接调用setState,触发页面无限重绘,导致闪屏
  2. TextButton.icon的label参数外错误包裹了Flexible组件,Flexible仅能作为Row/Column/Flex的直接子组件使用,父组件不匹配触发ParentData相关断言错误
  3. getCurrentUser方法中局部变量覆盖了类成员变量user,存在空值隐患

修复步骤

1. 移除build方法内的setState逻辑

删除build方法中如下代码段:

setState((){
  currentLocation = LatLng(
      locationData.latitude,
      locationData.longitude
  );
});

GoogleMap的initialCameraPosition参数可直接使用LatLng(locationData.latitude, locationData.longitude),无需额外存储到State中触发重绘。

2. 移除TextButton.icon的label外包裹的Flexible组件

修改前代码:

label: Flexible(
  child: Text(
   _locating ? 'Locating..'
       : locationData.selectedAddress == null?
   'Locating..':
   locationData.selectedAddress.featureName,
    overflow:TextOverflow.ellipsis,
    style: TextStyle(
        fontWeight:FontWeight.bold,
      fontSize: 20,
      color: Colors.black,
    ),
  ),
),

修改后代码:

label: Text(
 _locating ? 'Locating..'
     : locationData.selectedAddress == null?
 'Locating..':
 locationData.selectedAddress.featureName,
  overflow:TextOverflow.ellipsis,
  style: TextStyle(
      fontWeight:FontWeight.bold,
    fontSize: 20,
    color: Colors.black,
  ),
),

3. 修复getCurrentUser方法变量覆盖问题

修改前代码:

void getCurrentUser() {
  User user = FirebaseAuth.instance.currentUser;
  if(user!=null){
    setState((){
      _loggedIn=true;
      user= FirebaseAuth.instance.currentUser;
    });
  }
}

修改后代码:

void getCurrentUser() {
  User? currentUser = FirebaseAuth.instance.currentUser;
  if(currentUser!=null){
    setState((){
      _loggedIn = true;
      user = currentUser;
    });
  }
}

完成以上修改后,闪屏和断言错误都会被解决。

内容的提问来源于stack exchange,提问作者vansika

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 05:06:02