类型 'String' 不是类型 'ImageProvider<dynamic>' 的子类型
type 'String' is not a subtype of type 'ImageProvider<dynamic>'
我有类别列表,点击每个类别会在新屏幕中显示该类别的用户。新屏幕有一个 image
小部件和 text
小部件。如果用户有 profile picture
,并且没有与该用户关联的个人资料图片,则会显示不会引发此错误的默认图片,我会收到此错误。
Widget _getProfilePic(Pro pro) {
// If the pro has provided a profile picture
if(pro.profilePhoto.length > 0) {
// If the pro is available for a video call
if(statusMap[pro.onlineStatus] == "Online, available") {
return CircleAvatar(
radius: 30.0,
backgroundImage: pro.profilePhoto.startsWith("./profilepics") ? NetworkImage("" + pro.profilePhoto.substring(2)) : pro.profilePhoto);
}
// If pro is not available
else {
return CircleAvatar(
radius: 30.0,
backgroundImage: pro.profilePhoto.startsWith("./profilepics") ? NetworkImage("" + pro.profilePhoto.substring(2)) : pro.profilePhoto);
}
}
// Else, provide a default icon
else {
// If the pro is available for a video call
if(statusMap[pro.onlineStatus] == "Online, available") {
var profileImg = Image.asset('icons/default_profile_icon.png', height: 60.0);
return Image.asset('icons/default_profile_icon.png', height: 60.0);
}
// If the pro is not available
else {
return Image.asset('icons/default_profile_icon.png', height: 60.0);
}
}
这是Pro
class型号:
class Pro extends User {
String company;
String experience;
double rating;
int reviewCount;
int onlineStatus;
String proId;
Pro();
// Returns a Pro created from JSON
factory Pro.fromJson(Map<String, dynamic> json) {
Pro pro = Pro();
pro.proId = json['ProID'] as String;
pro.fullname = json['FullName'] as String;
pro.company = json['Company'] as String;
pro.experience = json['about'] as String;
pro.rating = json['ReviewAvg'] != null? double.tryParse(json['ReviewAvg']) : 0.0;
pro.reviewCount = json['ReviewCount'];
pro.onlineStatus = json['OnlineStatus'];
pro.profilePhoto = json['profile_pic'] as String;
return pro;
}
}
// Converts a http response body into a List<Pro>
List<Pro> parsePros(String responseBody) {
final parsed = json.decode(responseBody).cast<Map<String, dynamic>>();
return parsed.map<Pro>((json) => Pro.fromJson(json)).toList();
}
您可能需要在测试 ./profilepics
的两个地方将 pro.profileImage 包裹在 NetworkImage 中
我有类别列表,点击每个类别会在新屏幕中显示该类别的用户。新屏幕有一个 image
小部件和 text
小部件。如果用户有 profile picture
,并且没有与该用户关联的个人资料图片,则会显示不会引发此错误的默认图片,我会收到此错误。
Widget _getProfilePic(Pro pro) {
// If the pro has provided a profile picture
if(pro.profilePhoto.length > 0) {
// If the pro is available for a video call
if(statusMap[pro.onlineStatus] == "Online, available") {
return CircleAvatar(
radius: 30.0,
backgroundImage: pro.profilePhoto.startsWith("./profilepics") ? NetworkImage("" + pro.profilePhoto.substring(2)) : pro.profilePhoto);
}
// If pro is not available
else {
return CircleAvatar(
radius: 30.0,
backgroundImage: pro.profilePhoto.startsWith("./profilepics") ? NetworkImage("" + pro.profilePhoto.substring(2)) : pro.profilePhoto);
}
}
// Else, provide a default icon
else {
// If the pro is available for a video call
if(statusMap[pro.onlineStatus] == "Online, available") {
var profileImg = Image.asset('icons/default_profile_icon.png', height: 60.0);
return Image.asset('icons/default_profile_icon.png', height: 60.0);
}
// If the pro is not available
else {
return Image.asset('icons/default_profile_icon.png', height: 60.0);
}
}
这是Pro
class型号:
class Pro extends User {
String company;
String experience;
double rating;
int reviewCount;
int onlineStatus;
String proId;
Pro();
// Returns a Pro created from JSON
factory Pro.fromJson(Map<String, dynamic> json) {
Pro pro = Pro();
pro.proId = json['ProID'] as String;
pro.fullname = json['FullName'] as String;
pro.company = json['Company'] as String;
pro.experience = json['about'] as String;
pro.rating = json['ReviewAvg'] != null? double.tryParse(json['ReviewAvg']) : 0.0;
pro.reviewCount = json['ReviewCount'];
pro.onlineStatus = json['OnlineStatus'];
pro.profilePhoto = json['profile_pic'] as String;
return pro;
}
}
// Converts a http response body into a List<Pro>
List<Pro> parsePros(String responseBody) {
final parsed = json.decode(responseBody).cast<Map<String, dynamic>>();
return parsed.map<Pro>((json) => Pro.fromJson(json)).toList();
}
您可能需要在测试 ./profilepics
的两个地方将 pro.profileImage 包裹在 NetworkImage 中