Неудачное утверждение: строка 22 поз. 14: 'url! = Null': неверно - PullRequest
0 голосов
/ 21 февраля 2020

Я хочу показать изображение в виде списка с именем ProfileTile из сетевого изображения, и при запуске оно выдает мне следующую ошибку:

'package: flutter / src / painting / _network_image_io.dart': Ошибка утверждение: строка 22 поз. 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,
});
}

//profil list from 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;
}
}

1 Ответ

0 голосов
/ 21 февраля 2020

Я обнаружил проблему после печати

ProfileTile(profile: profiles[index])

В коллекции было несколько документов без поля imgUrl, поэтому я удалил их, и они работали нормально.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...