我想在网络图像的名为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;
}
}
答案 0 :(得分:3)
看起来问题是 profile.imgUrl 是null
,似乎您刚刚将null传递给了NetworkImage(...)
要修复以下问题
backgroundImage: (profile.imgUrl == null) ? AssetImage('images/user-avatar.png') : NetworkImage(profile.imgUrl)
答案 1 :(得分:1)
检查所有到Firebase的链接,然后检查那里的节点。 您可能放错了位置,即,当我尝试获取不存在的产品时,我的错误是null网址-由于错误而导致产品按订单错误。 firebaseio.com/products.json'; firebaseio.com/orders.json'; 我DID firebaseio.com/PRODUCTS/orders.json'; 这使订单成为产品,但订单没有url .....多数民众赞成在错误中。
答案 2 :(得分:0)
打印后我发现了问题
ProfileTile(profile: profiles[index])
集合中有一些文档没有imgUrl字段,所以我删除了它们并且工作正常。
答案 3 :(得分:0)
我试图将Firebaseuser的photoUrl存储在名为url的字符串中。但是问题是我在build方法中声明了它。