我正在尝试使用 flutter 构建一个社交媒体应用程序,我创建了一个登录页面和登录页面,它需要 userAvatar, 用户名,用户电子邮件,用户密码也是。当单击浮动操作按钮作为提交按钮时,它将注册帐户,但在我的情况下,它只注册电子邮件、用户名、用户名和用户密码,但不注册用户图像,它显示这个'package:flutter/src/paintings/_network_image_io.dart':失败的断言:第 25 行优点 14:'url != null':不是真的。导致小部件的相关错误是流构建器......这是我的错误......任何人都可以帮忙 我会在下面留下我的代码。
LandingServices 作为登录和登录
class LandingService with ChangeNotifier {
TextEditingController userEmailController = TextEditingController();
TextEditingController userNameController = TextEditingController();
TextEditingController userPasswordController = TextEditingController();
ConstantColors constantColors = ConstantColors();
showUserAvatar
showUserAvatar(BuildContext context) {
return showModalBottomSheet(
context: context,
builder: (context) {
return Container(
height: MediaQuery.of(context).size.height * 0.36,
width: MediaQuery.of(context).size.width,
child: Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 150.0),
child: Divider(
thickness: 4.0,
color: constantColors.whiteColor,
),
),
CircleAvatar(
radius: 69.0,
backgroundColor: constantColors.transperant,
backgroundImage: FileImage(
Provider.of<LandingUtils>(context, listen: false).userAvatar
)),
Container(
child:
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
MaterialButton(
child: Text(
'Reselect',
style: TextStyle(
color: constantColors.whiteColor,
fontWeight: FontWeight.bold,
decoration: TextDecoration.underline,
decorationColor: constantColors.whiteColor),
),
onPressed: () {
Provider.of<LandingUtils>(context, listen: false)
.pickUserAvatar(context, ImageSource.gallery);
}),
MaterialButton(
color: constantColors.blueColor,
child: Text(
'Confirm Image',
style: TextStyle(
color: constantColors.whiteColor,
fontWeight: FontWeight.bold,
),
),
onPressed: () {
Provider.of<FirebaseOperations>(context,
listen: false)
.uploadUserAvatar(context)
.whenComplete(() {
signInSheet(context);
});
}),
],
),
)
],
),
decoration: BoxDecoration(
color: constantColors.blueGreyColor,
borderRadius: BorderRadius.circular(15.0),
),
);
});
}
passswordLessSignIn
Widget passswordLessSignIn(BuildContext context) {
return SizedBox(
height: MediaQuery.of(context).size.height * 0.40,
width: MediaQuery.of(context).size.width,
child: StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection('users').snapshots(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
} else {
return new ListView(
children:
snapshot.data.docs.map((DocumentSnapshot documentSnapshot) {
return ListTile(
trailing: IconButton(
icon: Icon(FontAwesomeIcons.trashAlt,
color: constantColors.redColor),
onPressed: () {},
),
leading: CircleAvatar(
backgroundColor: constantColors.transperant,
backgroundImage:
NetworkImage(documentSnapshot.data()['userimage']),
),
subtitle: Text(documentSnapshot.data()['useremail'],
style: TextStyle(
fontWeight: FontWeight.bold,
color: constantColors.greenColor,
fontSize: 12.0)),
title: Text(documentSnapshot.data()['username'],
style: TextStyle(
fontWeight: FontWeight.bold,
color: constantColors.greenColor)),
);
}).toList());
}
},
),
);
}
signInSheet
signInSheet(BuildContext context) {
return showModalBottomSheet(
context: context,
isScrollControlled: true,
builder: (context) {
return Padding(
padding: EdgeInsets.only(
bottom: MediaQuery.of(context).viewInsets.bottom),
child: Container(
height: MediaQuery.of(context).size.height * 0.5,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
color: constantColors.blueGreyColor,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(12.0),
topRight: Radius.circular(12.0))),
child: Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 150.0),
child: Divider(
thickness: 4.0,
color: constantColors.whiteColor,
),
),
CircleAvatar(
backgroundImage: FileImage(
Provider.of<LandingUtils>(context, listen: false)
.getUserAvatar),
backgroundColor: constantColors.redColor,
radius: 60.0,
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: TextField(
controller: userNameController,
decoration: InputDecoration(
hintText: 'Enter name...',
hintStyle: TextStyle(
color: constantColors.whiteColor,
fontWeight: FontWeight.bold,
fontSize: 16.0,
),
),
style: TextStyle(
color: constantColors.whiteColor,
fontWeight: FontWeight.bold,
fontSize: 18.0,
),
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: TextField(
controller: userEmailController,
decoration: InputDecoration(
hintText: 'Enter Email...',
hintStyle: TextStyle(
color: constantColors.whiteColor,
fontWeight: FontWeight.bold,
fontSize: 16.0,
),
),
style: TextStyle(
color: constantColors.whiteColor,
fontWeight: FontWeight.bold,
fontSize: 18.0,
),
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: TextField(
controller: userPasswordController,
decoration: InputDecoration(
hintText: 'Enter Password...',
hintStyle: TextStyle(
color: constantColors.whiteColor,
fontWeight: FontWeight.bold,
fontSize: 16.0,
),
),
style: TextStyle(
color: constantColors.whiteColor,
fontWeight: FontWeight.bold,
fontSize: 18.0,
),
),
),
Padding(
padding: const EdgeInsets.only(top: 0.32),
child: FloatingActionButton(
backgroundColor: constantColors.redColor,
child: Icon(
FontAwesomeIcons.check,
color: constantColors.whiteColor,
),
onPressed: () {
if (userEmailController.text.isNotEmpty) {
Provider.of<Authentication>(context, listen: false)
.createAccount(userEmailController.text,
userPasswordController.text)
.whenComplete(() {
print('Creating collections...');
Provider.of<FirebaseOperations>(context,
listen: false)
.createUserCollection(context, {
'useruid': Provider.of<Authentication>(context,
listen: false)
.getUserUid,
'useremail': userEmailController.text,
'username': userNameController.text,
'userimage': Provider.of<LandingUtils>(context,
listen: false)
.getUserAvatarUrl,
});
}).whenComplete(() {
Navigator.pushReplacement(
context,
PageTransition(
child: Homepage(),
type: PageTransitionType.bottomToTop));
});
} else {
warningText(context, 'Fill all the data!');
}
}),
),
],
),
),
);
});
}
firebaseOperation.dart
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:theGupshup/screens/LandingPage/LandingUtils.dart';
import 'package:theGupshup/services/Authentication.dart';
class FirebaseOperations with ChangeNotifier {
UploadTask imageUploadTask;
String initUserName, initUserEmail, initUserImage;
String get getInitUserName => initUserName;
String get getInitUserEmail => initUserEmail;
String get getInitUserImage => initUserImage;
Future uploadUserAvatar(BuildContext context) async {
Reference imageReference = FirebaseStorage.instance.ref().child(
'userProfileAvatar/${Provider.of<LandingUtils>(context, listen:
false).getUserAvatar.path}/${TimeOfDay.now()}');
imageUploadTask = imageReference.putFile(
Provider.of<LandingUtils>(context, listen: false).getUserAvatar);
await imageUploadTask.whenComplete(() {
print('Image uploaded!');
});
imageReference.getDownloadURL().then((url) {
Provider.of<LandingUtils>(context, listen: false).userAvatarUrl =
url.toString();
print(
'the user profile avatar url => ${Provider.of<LandingUtils>(context, listen:
false).userAvatarUrl}');
notifyListeners();
});
}
Future createUserCollection(BuildContext context, dynamic data) async {
return FirebaseFirestore.instance
.collection('users')
.doc(Provider.of<Authentication>(context, listen: false).getUserUid)
.set(data);
}
Future initUserData(BuildContext context) async {
return FirebaseFirestore.instance
.collection('users')
.doc(Provider.of<Authentication>(context, listen: false).getUserUid)
.get()
.then((doc) {
print('Fetching user data');
initUserName = doc.data()['username'];
initUserEmail = doc.data()['useremail'];
initUserImage = doc.data()['userimage'];
print(initUserName);
print(initUserEmail);
print(initUserImage);
notifyListeners();
});
}
}
登陆utils.dart
class LandingUtils with ChangeNotifier {
ConstantColors constantColors = ConstantColors();
final picker = ImagePicker();
File userAvatar;
File get getUserAvatar => userAvatar;
String userAvatarUrl;
String get getUserAvatarUrl => userAvatarUrl;
Future pickUserAvatar(BuildContext context, ImageSource source) async {
final pickedUserAvatar = await picker.getImage(source: source);
pickedUserAvatar == null
? print('Select image')
: userAvatar = File(pickedUserAvatar.path);
print(userAvatar.path);
userAvatar != null
? Provider.of<LandingService>(context, listen: false)
.showUserAvatar(context)
: print('Image Upload error');
notifyListeners();
}
Future selectAvatarOptionSheet(BuildContext context) async {
return showModalBottomSheet(
context: context,
builder: (context) {
return Container(
child: Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 150.0),
child: Divider(
thickness: 4.0,
color: constantColors.whiteColor,
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
MaterialButton(
color: constantColors.blueColor,
child: Text('Gallery',
style: TextStyle(
color: constantColors.whiteColor,
fontWeight: FontWeight.bold,
fontSize: 18.0)),
onPressed: () {
pickUserAvatar(context, ImageSource.gallery)
.whenComplete(() {
Navigator.pop(context);
Provider.of<LandingService>(context, listen: false)
.showUserAvatar(context);
});
}),
MaterialButton(
color: constantColors.blueColor,
child: Text('Camera',
style: TextStyle(
color: constantColors.whiteColor,
fontWeight: FontWeight.bold,
fontSize: 18.0)),
onPressed: () {
pickUserAvatar(context, ImageSource.camera)
.whenComplete(() {
Navigator.pop(context);
Provider.of<LandingService>(context, listen: false)
.showUserAvatar(context);
});
}),
],
),
],
),
height: MediaQuery.of(context).size.height * 0.1,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
color: constantColors.blueGreyColor,
borderRadius: BorderRadius.circular(12.0)),
);
});
}
}
任何人都可以帮助下面的错误图片
谁能帮忙
答案 0 :(得分:0)
在您的 passwordLessSignIn 页面中,当 userimage 为 null 时,尝试为圆形头像提供默认图像 -
示例 -
leading: CircleAvatar(
backgroundColor: constantColors.transperant,
backgroundImage:
NetworkImage(documentSnapshot.data()['userimage'] ?? "https://i.guim.co.uk/img/media/7a633730f5f90db3c12f6efc954a2d5b475c3d4a/0_138_5544_3327/master/5544.jpg?width=1200&height=1200&quality=85&auto=format&fit=crop&s=27c09d27ccbd139fd0f7d1cef8f7d41d"),
),