Flutter滑动Google地图出现ParentDataElement断言错误如何解决?
Flutter Google地图滑动底部区域闪屏及断言错误修复方案
问题表现
移动Google地图时,更新后的位置信息可正常展示在底部容器中,但向上滑动位置信息区域时页面出现闪屏,同时抛出如下断言错误:Failed assertion: line 4973 pos 16: 'child is! ParentDataElement<ParentData>': is not true
错误原因
- 在
build方法中直接调用setState,触发页面无限重绘,导致闪屏 TextButton.icon的label参数外错误包裹了Flexible组件,Flexible仅能作为Row/Column/Flex的直接子组件使用,父组件不匹配触发ParentData相关断言错误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
相关产品推荐
相关产品推荐

