【问题标题】:Failed assertion: line 22 pos 14: 'url != null': is not true断言失败:第 22 行 pos 14: 'url != null': is not true
【发布时间】: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


【解决方案1】:

看起来问题是 profile.imgUrlnull 并且您似乎只是将 null 传递给了 NetworkImage(...)

在下面修复它

backgroundImage: (profile.imgUrl == null) ? AssetImage('images/user-avatar.png') : NetworkImage(profile.imgUrl)

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      检查所有指向 firebase 的链接并检查那里的节点。 您可能放错了地方,即我的错误是在尝试获取不存在的产品时的空 url - 由于错误而误按产品订购。 firebaseio.com/products.json'; firebaseio.com/orders.json'; 我做到了 firebaseio.com/PRODUCTS/orders.json'; 这使得订单成为产品,但订单没有 url .....这是错误。

      【讨论】:

        【解决方案4】:

        打印后发现问题

        ProfileTile(profile: profiles[index])
        

        集合中有一些文档没有 imgUrl 字段,所以我删除了它们,它工作正常。

        【讨论】:

          猜你喜欢
          • 2020-07-01
          • 2021-03-07
          • 1970-01-01
          • 2020-11-24
          • 2020-11-30
          • 1970-01-01
          • 1970-01-01
          • 2020-04-03
          • 1970-01-01
          相关资源
          最近更新 更多