I am following a youtube tutorial and despite following the tutorial closely I have an error on ( _image = null; and File _image; ) Not sure what is wrong if anyone knows please comment. I tried everything and its stressing me out. I have the link to yt vid:
class _NoteEditScreenState extends State {
final titleController = TextEditingController();
final contentController = TextEditingController();
File _image;
final picker = ImagePicker();
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: white,
appBar: AppBar(elevation: 0.7,
backgroundColor: Colors.white,
leading: IconButton(onPressed: () => Navigator.of(context).pop(),
icon: Icon(Icons.arrow_back),
color: Colors.black,
),
actions: [
IconButton(
icon: Icon(Icons.photo_camera),
color: Colors.black,
onPressed: () {
//getImage(ImageSource.camera);
},
),
IconButton(
icon: Icon(Icons.insert_photo),
color: Colors.black,
onPressed: () {
//getImage(ImageSource.gallery);
},
),
IconButton(
icon: Icon(Icons.delete),
color: Colors.black,
onPressed: () {
Navigator.pop(context);
},
),
],
),
body: SingleChildScrollView(
child: Column(
children: [
Padding(
padding: EdgeInsets.only(
left: 10.0, right: 5.0, top: 10.0, bottom: 5.0),
child: TextField(
controller: titleController,
maxLines: null,
textCapitalization: TextCapitalization.sentences,
style: createTitle,
decoration: InputDecoration(
hintText: 'Enter Note Title', border: InputBorder.none),
),
),
if(_image != null)
Container(
padding: EdgeInsets.all(10.0),
width: MediaQuery.of(context).size.width,
height: 250.0,
child: Stack(
children: [
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20.0),
image: DecorationImage(
image: FileImage(_image),
fit: BoxFit.cover,
),
),
),
Align(
alignment: Alignment.bottomRight,
child: Padding(
padding: EdgeInsets.all(12.0),
child: Container(
height: 30.0,
width: 30.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.white,
),
child: InkWell(
onTap: () {
setState((){
_image = null;
},
);
},
child: Icon(
Icons.delete,
size: 16.0,
),
),
),
),
)
],
),
),
],
),
),
);
}
void saveNote() {
String title = titleController.text.trim();
String content = contentController.text.trim();
String imagePath = _image != null ? _image.path : null;
int id = DateTime.now().millisecondsSinceEpoch;
Provider.of<NoteProvider>(this.context, listen: false).addOrUpdateNote(id, title, content, imagePath, EditMode.ADD);
Navigator.of(this.context).pushReplacementNamed(NoteViewScreen.route, arguments: id);
}
}
CodePudding user response:
Also note that when declaring a variable, its value can be null. For this ? character:
File? _image;
CodePudding user response:
I am following a youtube tutorial
That tutorial is from November 2020 and uses Flutter 1. That is somewhat recent, but Flutter really moves at high speed and makes progress fast. In March of 2021 the "sound null safety" feature was officially released with Flutter 2 (we are on Flutter 3 right now by the way).
So what I would suggest, instead of asking a lot of specific questions about the outdated tutorial and getting a band aid solution for a tiny bit of it each time you ask, do two things:
- Read about null-safety in Dart and Flutter:
It is a great feature, that will help you a lot.
- https://dart.dev/null-safety
- https://dart.dev/null-safety/understanding-null-safety
- dart-null-safety
- Go find a tutorial for the current version of Dart and Flutter. At least one that came out this year. For a technology as fast moving as Flutter, a tutorial from 18 months ago is about as useful as a tutorial on how to operate a fax machine. Might not have been wrong in their time, but certainly confusing to read today.
Oh, yes, the bandaid fix for that one error is to mark the variable as nullable, by adding a ?
to the declaration. File? _image;
. But then you will get just a ton of followup errors. Don't go down that rabbit hole. Learn how to do it properly instead.