断言失败:第 22 行 pos 14:"url != null":不正确



我想在一个名为ProfileFile的列表视图磁贴中显示来自网络图像的图像,当它运行时,它会给我这个错误:

"package:flatt/src/painting/network_image_io.dart":断言失败:第22行位置14:"url!=null":不为真。导致错误的相关小部件是:ProfileFilefile:///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]);
},

ProfileFile类如下所示:

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;
}
}

检查所有到firebase的链接并检查那里的节点。你可能放错地方了,比如我在尝试获取不存在的产品时的错误是一个空url——这是因为一个错误而导致产品订单错误。firebaseio.com/products.json';firebaseio.com/orders.json';我做到了firebaseio.com/PRODUCTS/orders.json';这使得订单成为产品,但订单没有url。。。。。这就是错误。

打印后发现问题

ProfileTile(profile: profiles[index])

集合中有一些文档没有imgUrl字段,所以我删除了它们,效果很好。

我试图将Firebaseuser的照片url存储在一个名为url的字符串中。但问题是我在构建方法中声明了它。

最新更新