【发布时间】:2020-06-05 19:32:56
【问题描述】:
我想在一个名为 ProfileTile 的列表视图图块中显示来自网络图像的图像,当它运行时它给我这个错误:
'package:flutter/src/painting/_network_image_io.dart':断言失败:第 22 行 pos 14:'url != null':不正确。 相关的导致错误的小部件是: ProfileTile 文件:///Users/ahmed/AndroidStudioProjects/flutter_app_service2/lib/screens/home/profile_list.dart:28:16
我定义profile.dart如下
return ListView.builder(
itemBuilder: (context, index) {
return ProfileTile(profile: profiles[index]);
},
ProfileTile 类是这样的:
class ProfileTile extends StatelessWidget {
final Profile profile;
ProfileTile({this.profile});
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.only(top: 8.0),
child: Card(
margin: EdgeInsets.fromLTRB(20.0, 6.0, 20.0, 0.0),
child: ListTile(
leading: CircleAvatar(
backgroundImage: NetworkImage(profile.imgUrl),
radius: 25.0,
),
title: Text(profile.firstName + ' ' + profile.lastName),
subtitle: Text(profile.city + ' ' + profile.country),
),
),
);
} }
数据库文件如下:
class DatabaseService {
final String uid;
DatabaseService({this.uid});
//collection reference
final CollectionReference profileCollection =
Firestore.instance.collection('profiles');
Future updateUserData(String firstName, String lastName, String country,
String city, String imgUrl) async {
return await profileCollection.document(uid).setData({
'firstName': firstName,
'lastName': lastName,
'country': country,
'city': city,
'imgUrl': imgUrl,
});
}
//profile list from a snapshot
List<Profile> _profileListFromSnapshot(QuerySnapshot snapshot) {
return snapshot.documents.map((doc) {
return Profile(
firstName: doc.data['firstName'] ?? '',
lastName: doc.data['lastName'] ?? '',
country: doc.data['country'] ?? '',
city: doc.data['city'] ?? '',
imgUrl: doc.data['imgUrl'],
);
}).toList();
}
//get profiles list
Stream<List<Profile>> get profiles {
return profileCollection.snapshots().map(_profileListFromSnapshot);
}
}
我将默认值放在 auth.dart 文件中,如下所示:
Future registerWithEmailAndPassword(String email, String password) async {
try {
AuthResult result = await _auth.createUserWithEmailAndPassword(
email: email, password: password);
FirebaseUser user = result.user;
//create new document for the user with uid
await DatabaseService(uid: user.uid).updateUserData(
'Ahmed', 'Hussein', 'Alexandria', 'Egypt', 'https://cdn.vox-cdn.com/thumbor/BmvVMEzNQQ4rfIQXput2yOriDRc=/0x0:5568x3712/1820x1213/filters:focal(2858x720:3748x1610):format(webp)/cdn.vox-cdn.com/uploads/chorus_image/image/62207705/922984782.jpg.0.jpg');
return _userFromFirebaseUser(user);
} catch (e) {
print(e.toString());
return null;
}
}
【问题讨论】:
-
这个
profile.imgUrl为空 -
您能告诉我如何解决吗?因为它是“最终的”,我无法为其设置默认值
-
你在这里给它一个值
ProfileTile(profile: profiles[index])你需要打印它并检查它包含的内容 -
这里是 Profile 类:'class Profile { final String firstName;最终字符串姓氏;最终字符串国家;最终字符串城市;最终字符串 imgUrl;个人资料({this.firstName,this.lastName,this.country,this.city,this.imgUrl}); }'
-
谢谢@PeterHaddad。打印后发现问题是集合中有一些文档没有 imgUrl 字段,所以我删除了它们,它工作正常。
标签: flutter listview dart null flutter-layout