I'm trying to save a photo profile for the user of my application with a long process with id and everything, I followed a tutorial but I'm having errors. Thank you for helping me !
So this is my code in my profile edit screen:
class ProfileEditScreen extends StatefulWidget {
@override
_ProfileEditScreenState createState() => _ProfileEditScreenState();
}
class _ProfileEditScreenState extends State<ProfileEditScreen> {
UserModel? currentUser;
@override
Widget build(BuildContext context) {
Size size = MediaQuery
.of(context)
.size;
return Scaffold(
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Colors.tealAccent, Colors.lightBlue.shade900]
),
),
width: double.infinity,
height: size.height,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Avatar(
avatarUrl: currentUser?.avatarUrl,
onTap: () {},
),
SizedBox(height: 75),
Container(
margin: EdgeInsets.symmetric(vertical: 10),
padding: EdgeInsets.symmetric(horizontal: 20, vertical: 5),
width: size.width * 0.8,
decoration: BoxDecoration(
color: OpacWhite,
borderRadius: BorderRadius.circular(29),
),
child: TextFormField(
decoration: InputDecoration(
hintText: "Username",
border: InputBorder.none,
icon: Icon(
Icons.person,
color: DarkTurquoise,))
)
),
Container(
margin: EdgeInsets.symmetric(vertical: 10),
width: size.width * 0.8,
child: ClipRRect(
borderRadius: BorderRadius.circular(29),
// ignore: deprecated_member_use
child: FlatButton(
padding: EdgeInsets.symmetric(
vertical: 18, horizontal: 40),
color: DarkTurquoise,
onPressed: () =>
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => NavScreen(),),
),
child: Text(
"SAVE", style: TextStyle(color: SandYellow,
fontSize: 16,),
)
)
)
)
]
),
),
);
}
}
My code in my User Model
class UserModel {
String uid;
String displayName;
String avatarUrl;
UserModel(this.uid, {required this.displayName, required this.avatarUrl});
}
finally I have this error: Error: The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nullable and 'String' isn't. avatarUrl: currentUser?.avatarUrl,
This is the backend of Avatar();
import 'package:flutter/material.dart';
class Avatar extends StatelessWidget{
final String avatarUrl;
final VoidCallback onTap;
const Avatar({required this.avatarUrl, required this.onTap});
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onTap,
child: avatarUrl = null \\I have error there too, they want me to put late before string but it doesn't works
? CircleAvatar(
backgroundImage: NetworkImage ("https://www.villascitemirabel.com/wp-content/uploads/2016/07/default-profile.png"),
radius: 90.0,
child: Icon(Icons.photo_camera, size: 45),
)
:CircleAvatar(
radius: 50.0,
backgroundImage: NetworkImage(avatarUrl)
),
);
}
}
Means that your variable is a non nullable UserModel. If you want to allow
currentUserto be null,https://dart.dev/codelabs/dart-cheatsheet