I have been trying to finish navigation for my flutter app, but I keep getting "NoSuchMethodError: Closure call with mismatched arguments: function' _CameraPageState.takePicture..". This error occurs after taking a picture and using the use on-pressed on AddNewBuddyPage to create an object add to my List on DashboardPage. Below is the class & function where the error is coming from.
class CameraPage extends StatefulWidget {
const CameraPage({Key? key, required this.cameras}) : super(key: key);
final List<CameraDescription>? cameras;
@override
State<CameraPage> createState() => _CameraPageState();
}
class _CameraPageState extends State<CameraPage> {
late CameraController _cameraController;
bool _isRearCameraSelected = true;
@override
void dispose() {
_cameraController.dispose();
super.dispose();
}
@override
void initState() {
super.initState();
initCamera(widget.cameras![0]);
}
Future takePicture() async {
if (!_cameraController.value.isInitialized) {
return null;
}
if (_cameraController.value.isTakingPicture) {
return null;
}
try {
await _cameraController.setFlashMode(FlashMode.off);
XFile picture = await _cameraController.takePicture();
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
AddNewBuddyPage(addBuddy: () {}, image: picture)));
} on CameraException catch (e) {
debugPrint('Error occured while taking picture: $e');
return null;
}
}
Future initCamera(CameraDescription cameraDescription) async {
_cameraController =
CameraController(cameraDescription, ResolutionPreset.high);
try {
await _cameraController.initialize().then((_) {
if (!mounted) return;
setState(() {});
});
} on CameraException catch (e) {
debugPrint("camera error $e");
}
}
I have no issue generating the picture and passing it to AddNewBuddyPage, but once I click on the button to create a new Object of type PlantBuddyModel I cannot navigate to the DashboardPage due to "NoSuchMethodError" error.
class AddNewBuddyPage extends StatefulWidget {
final Function addBuddy;
XFile image;
AddNewBuddyPage({super.key, required this.addBuddy, required this.image});
@override
State<AddNewBuddyPage> createState() => _AddNewBuddyPageState();
}
class _AddNewBuddyPageState extends State<AddNewBuddyPage> {
final plantNameController = TextEditingController();
final heightController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(
automaticallyImplyLeading: false,
title: const Text('Create New Plant Buddy'),
),
body: SingleChildScrollView(
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(10),
child: TextField(
obscureText: true,
controller: plantNameController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Plant Name',
hintText: 'Enter your Plant\'s Name'),
),
),
Padding(
padding: const EdgeInsets.all(10),
child: TextField(
controller: heightController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Plant Height',
hintText: 'Enter your Plant\'s Height'),
),
),
ElevatedButton(
onPressed: () async {
await availableCameras().then((value) => Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CameraPage(cameras: value))));
},
child: const Text("Take a Picture"),
),
Image.file(
File(widget.image!.path),
fit: BoxFit.cover,
width: 250,
height: 200,
),
Container(
height: 50,
width: 250,
decoration: BoxDecoration(
color: Colors.blue, borderRadius: BorderRadius.circular(20)),
child: TextButton(
onPressed: () {
widget.addBuddy(PlantDataModel(
plantName: plantNameController.text,
height: double.parse(heightController.text),
date: DateFormat.yMMMd().format(DateTime.now()),
id: DateTime.now().toString(), image: widget.image,
));
Navigator.pop(context);
},
child: const Text(
'Add Buddy',
style: TextStyle(color: Colors.white, fontSize: 25),
),
),
)
],
),
),
);
}
}
Below is the function responsible for adding objects to the List on DashboardPage below & the model.
class _DashboardPageState extends State<DashboardPage> {
final List<PlantDataModel> currentPlantBuddyList = [
];
late XFile image = XFile('path');
void _addNewPlantBuddy(PlantDataModel buddy) {
setState(() {
image = XFile(buddy.image.path);
currentPlantBuddyList.add(buddy);
});
}
class PlantDataModel extends StatelessWidget {
final String plantName;
final double height;
final String date;
final String id;
final XFile image;
PlantDataModel(
{required this.plantName,
required this.height,
required this.date,
required this.id, required this.image, });
If you could please help I would appreciate it. New to flutter :(