hope all are doing well
i am trying to use camera and add picture from it to a list.i can review the image but whe i pressed the floating button to save it i got this error:
Cannot open file, path = 'Instance of 'Place'' (OS Error: No such file or directory, errno = 2)
i used these three packages to camera usage:
image_picker,
path ,
path_provider
hope if there any solution please
image Input where the function _takePicture is :
import 'package:flutter/material.dart';
import 'dart:io';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart' as syspath;
import 'package:image_picker/image_picker.dart';
class ImageInput extends StatefulWidget {
const ImageInput({super.key, required this.onselectImage});
final Function onselectImage;
@override
State<ImageInput> createState() => _ImageInputState();
}
class _ImageInputState extends State<ImageInput> {
File? _storedImage;
Future<void> _takePicture() async {
final picker = ImagePicker();
final imageFile = await picker.pickImage(
source: ImageSource.camera,
maxWidth: 600,
);
final imageInverted = File(imageFile!.path);
setState(() {
_storedImage = imageInverted;
});
final appDir = await syspath.getApplicationDocumentsDirectory();
final filename = path.basename(imageFile.path);
final savedImage = await imageInverted.copy('${appDir.path}/$filename');
widget.onselectImage(savedImage);
}
@override
Widget build(BuildContext context) {
return Row(
children: [
Container(
height: 150,
width: 250,
decoration:
BoxDecoration(border: Border.all(width: 1, color: Colors.grey)),
alignment: Alignment.center,
child: _storedImage != null
? Image.file(
_storedImage!,
fit: BoxFit.cover,
width: double.infinity,
)
: const Text(
'No Image Taken!!',
textAlign: TextAlign.center,
),
),
const SizedBox(
height: 10,
),
Expanded(
child: TextButton.icon(
onPressed: _takePicture,
icon: const Icon(Icons.camera),
label: const Text('Take Picture'),
),
)
],
);
}
}
function of Saving image:
class AddPlaceScreen extends StatefulWidget {
static const routeName = '/add-place';
const AddPlaceScreen({super.key});
@override
State<AddPlaceScreen> createState() => _AddPlaceScreenState();
}
class _AddPlaceScreenState extends State<AddPlaceScreen> {
final _titleController = TextEditingController();
File? _pickedImage;
void _selectImage(File pickedImage) {
_pickedImage = pickedImage;
}
void _savePlace() {
if (_titleController.text.isEmpty || _pickedImage == null) {
return;
}
Provider.of<GreatPlaces>(context, listen: false)
.addPlace(_titleController.text, _pickedImage!);
Navigator.of(context).pop();
}
floatingActionButton: FloatingActionButton(
onPressed: _savePlace,
elevation: 0,
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
backgroundColor: Colors.deepOrange,
child: const Icon(Icons.add),
),
Model place:
import 'dart:io';
class PlaceLocation {
PlaceLocation({
required this.latitude,
required this.longitude,
this.address = '',
});
final double latitude;
final double longitude;
final String address;
}
class Place {
Place({
required this.id,
required this.title,
required this.location,
required this.image,
});
final String id;
final String title;
final PlaceLocation location;
final File image;
}
provider :
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:greatplaces/models/place.dart';
class GreatPlaces with ChangeNotifier {
final List<Place> _items = [];
List<Place> get items {
return [..._items];
}
void addPlace(String pickedTitle, File pickedImage) {
final newPlace = Place(
id: DateTime.now().toString(),
title: pickedTitle,
location: PlaceLocation(address: '', latitude: 0, longitude: 0),
image: pickedImage,
);
_items.add(newPlace);
notifyListeners();
}
}