项目场景:
注意: Flutter 版本是 3.32.6 dart 版本是 3.8.1
Flutter app 问题记录
问题一
The following NoSuchMethodError was thrown while handling a gesture:
‘title’
method not found
Receiver: Instance of ‘IdentityMap<String, String>’
Arguments: []
修改前
if(itemVale.title == '我的房屋'){
Navigator.pushNamed(context, '/roomPage');
}else if(itemVale.title == '我的报修'){
}else if(itemVale.title == '访客记录'){
}
修改后
if(itemVale['title'] == '我的房屋'){
Navigator.pushNamed(context, '/roomPage');
}else if(itemVale['title'] == '我的报修'){
}else if(itemVale['title'] == '访客记录'){
}
问题二
Assertion failed: file:///D:/flutter/flutter/packages/flutter/lib/src/ widgets/container.dart:276:10 colornull || decorationnull "Cannot provide both a color and a decorationinTo provide both, use l’decoration: BoxDecoration(color. color)\ See also: https://docs.flutter.dev/testing/errors
修改前
Container(
color: Colors.white,
width: MediaQuery.of(context).size.width * 0.9,
height: 120,
padding: EdgeInsetsGeometry.all(10),
decoration: BoxDecoration(
borderRadius:BorderRadius.only(
topLeft:Radius.circular(10.0),
topRight:Radius.circular(10.0),
bottomLeft:Radius.circular(10.0),
bottomRight:Radius.circular(10.0),
)
),
修改后
Container(
width: MediaQuery.of(context).size.width * 0.9,
height: 120,
padding: EdgeInsetsGeometry.all(10),
decoration: BoxDecoration(
color: Colors.white,
borderRadius:BorderRadius.only(
topLeft:Radius.circular(10.0),
topRight:Radius.circular(10.0),
bottomLeft:Radius.circular(10.0),
bottomRight:Radius.circular(10.0),
)
),
问题三
The method ‘catchError’ isn’t defined for the type ‘NetworkImage’. (Documentation)
Try correcting the name to the name of an existing method, or defining a method named ‘catchError’. dart 版本 3.8.1
修改之前
Image(
width: 50,
image: NetworkImage(userInfo['avatar'] ?? '')
.catchError((_) =>
AssetImage('assets/images/avatar_1.jpg')),
)
修改后
// 使用方法
buildAvatar(userInfo['avatar'] as String?)
// 组件方法
Widget buildAvatar(String? avatarUrl) {
// 验证头像URL是否有效
final bool hasValidAvatar = avatarUrl != null && avatarUrl.isNotEmpty;
return Image(
width: 50,
// 根据URL是否有效选择图片源
image: hasValidAvatar
? NetworkImage(avatarUrl)
: const AssetImage('assets/images/avatar_1.jpg'),
// 图片加载过程中显示占位图
loadingBuilder: (context, child, progress) {
if (progress == null) {
return child; // 加载完成,显示图片
}
// 加载中显示默认头像
return Image.asset(
'assets/images/avatar_1.jpg',
width: 50,
);
},
// 图片加载失败时显示默认头像
errorBuilder: (context, error, stackTrace) {
// 可以在这里添加错误日志记录
// debugPrint('头像加载失败: $error');
return Image.asset(
'assets/images/avatar_1.jpg',
width: 50,
);
},
// 图片解码失败时的备选方案
frameBuilder: (context, child, frame, wasSynchronouslyLoaded) {
if (wasSynchronouslyLoaded) {
return child;
}
return AnimatedOpacity(
child: child,
opacity: frame == null ? 0 : 1,
duration: const Duration(milliseconds: 200),
curve: Curves.easeOut,
);
},
);
}
结尾
日常开发问题记录,会持续跟新,欢迎大家投稿谢谢,共同进步