I am very new to Dart, and coding in general. I have produced this code after watching tutorials on YouTube. For the most part, I have been able to troubleshoot most of my problems on my own, here I feel I need some help. I have written code to upload photographs, and trying to use the functionality on another page, but it is not working. I need Help
Here is my Code:-
import 'dart:io';
import 'package:flutter/services.dart';
import '';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:lms_definer/controllers/profileController.dart';
import 'package:lms_definer/controllers/textController.dart';
import 'package:get/get.dart';
import 'package:lms_definer/model/profileModel.dart';
import 'package:lms_definer/constants/constants.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:lms_definer/screens/screens.dart';
import '../helper/firestore_db.dart';
import 'package:lms_definer/widgets/photo.dart';
class EditProfile extends StatefulWidget {
EditProfile({Key? key}) : super(key: key);
@override
State<EditProfile> createState() => _EditProfileState();
}
class _EditProfileState extends State<EditProfile> {
@override
Widget build(BuildContext context) {
var _textController = ProfileEdit();
final PhotoUpload photo = PhotoUpload();
return Scaffold(
body: GetX<ProfileController>(
init: Get.put<ProfileController>(ProfileController()),
builder: (ProfileController profileController) {
return Container(
child: ListView.builder(
itemCount: profileController.profiles.length,
itemBuilder: (BuildContext context, int i) {
final _profileModel = profileController.profiles[i];
setTextEditControllerValue(_textController, _profileModel);
return SafeArea(
child: Container(
padding: EdgeInsets.all(20),
child: Form(
child: Column(
children: [
const SizedBox(
height: 20,
),
GestureDetector(
onTap: () {
photo.photoBottomSheet();
},
child: CircleAvatar(
radius: 100,
backgroundImage: NetworkImage(
'https://www.alchinlong.com/wp-content/uploads/2015/09/sample-profile.png'),
),
),
const SizedBox(
height: 40,
),
TextFormField(
decoration: const InputDecoration(
labelText: 'First Name',
border: OutlineInputBorder(
borderSide: BorderSide()),
),
controller: _textController.fNameController),
SizedBox(
height: 10,
),
TextFormField(
decoration: const InputDecoration(
labelText: 'First Name',
border:
OutlineInputBorder(borderSide: BorderSide()),
),
controller: _textController.lNameController,
),
SizedBox(
height: 10,
),
TextFormField(
decoration: const InputDecoration(
labelText: 'Address',
border:
OutlineInputBorder(borderSide: BorderSide()),
),
controller: _textController.adressController,
),
SizedBox(
height: 10,
),
TextFormField(
decoration: const InputDecoration(
labelText: 'Phone Numbaer',
border:
OutlineInputBorder(borderSide: BorderSide()),
),
controller: _textController.phoneController,
),
SizedBox(
height: 10,
),
TextFormField(
decoration: const InputDecoration(
labelText: 'School Name',
border:
OutlineInputBorder(borderSide: BorderSide()),
),
controller: _textController.sclNameController,
),
SizedBox(
height: 10,
),
TextFormField(
decoration: const InputDecoration(
labelText: 'Student Class',
border:
OutlineInputBorder(borderSide: BorderSide()),
),
controller: _textController.stdClassController,
),
SizedBox(
height: 10,
),
ElevatedButton(
onPressed: () async {
CircularProgressIndicator();
ProfileModel profileModel =
profileModelVal(_textController);
await FirestoreDb.updateProfile(profileModel);
Get.offAll(ProfileScreen());
},
child: Text('Update'))
],
),
),
),
);
}),
);
},
));
}
ProfileModel profileModelVal(ProfileEdit _textController) {
final profileModel = ProfileModel(
firstName: _textController.fNameController.text.trim(),
lastName: _textController.lNameController.text.trim(),
parentName: _textController.fatherNameController.text.trim(),
phoneNumber: _textController.phoneController.text.trim(),
address: _textController.adressController.text.trim(),
schoolName: _textController.sclNameController.text.trim(),
stdClass: _textController.stdClassController.text.trim());
return profileModel;
}
void setTextEditControllerValue(
ProfileEdit _textController, ProfileModel _profileModel) {
_textController.fNameController.value =
TextEditingValue(text: _profileModel.firstName);
_textController.lNameController.value =
TextEditingValue(text: _profileModel.lastName);
_textController.fatherNameController.value =
TextEditingValue(text: _profileModel.parentName);
_textController.adressController.value =
TextEditingValue(text: _profileModel.address);
_textController.phoneController.value =
TextEditingValue(text: _profileModel.phoneNumber);
_textController.sclNameController.value =
TextEditingValue(text: _profileModel.schoolName);
_textController.stdClassController.value =
TextEditingValue(text: _profileModel.stdClass);
}
void clearTextController(ProfileEdit _textController) {
_textController.fNameController.clear();
_textController.lNameController.clear();
_textController.fatherNameController.clear();
_textController.adressController.clear();
_textController.phoneController.clear();
_textController.sclNameController.clear();
_textController.stdClassController.clear();
}
}
The file Upload Code on there page is :-
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:io';
import 'package:get/get.dart';
import 'package:lms_definer/screens/editProfile.dart';
class PhotoUpload{
Future PickedImage(ImageSource source) async {
try {
final image = await ImagePicker()
.pickImage(source: source, maxWidth: 160, maxHeight: 160);
if (image == null) return;
final _imgTemp = File(image.path);
} on PlatformException catch (e) {
print('failed to Upload $e');
}
photoBottomSheet() {
Get.bottomSheet(
Container(
//color: Colors.white,
height: 150,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment:
CrossAxisAlignment.center,
children: [
//Text('Select from source'),
GestureDetector(
onTap: () =>
PickedImage(ImageSource.camera),
child: Row(
mainAxisAlignment:
MainAxisAlignment.center,
children: [
Icon(
Icons.camera,
size: 25,
color: Colors.white,
),
SizedBox(
width: 10,
),
Text(
'Camera',
style: TextStyle(fontSize: 25, color: Colors.white),
),
],
),
),
SizedBox(
height: 10,
),
Divider(
thickness: 0.5, color: Colors.white,
),
SizedBox(
height: 10,
),
GestureDetector(
onTap: () =>
PickedImage(ImageSource.gallery),
child: Row(
mainAxisAlignment:
MainAxisAlignment.center,
children: [
Icon(
Icons.image,
size: 25,
color: Colors.white,
),
SizedBox(
width: 10,
),
Text(
'Gallery',
style: TextStyle(fontSize: 25, color: Colors.white),
),
],
),
),
],
),
),
backgroundColor: Color(0xff2AA8A1),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
);
}
}
}
The error I am getting is:-
Syncing files to device Android SDK built for x86...
lib/screens/editProfile.dart:69:45: Error: The method 'photoBottomSheet' isn't defined for the class 'PhotoUpload'.
- 'PhotoUpload' is from 'package:lms_definer/widgets/photo.dart'
('lib/widgets/photo.dart').
Try correcting the name to the name of an existing method, or defining a method named
'photoBottomSheet'.
photo.photoBottomSheet();
^^^^^^^^^^^^^^^^
The idea is to use this functionality as a widget and the same code can be used in multiple places. But I am unable to solve the issue by myself with my limited knowledge. Please help.
CodePudding user response:
You could use a state-management-package. Provider is recommended by flutter and very simple to learn.
- get the latest version of provider from pub.dev (provider: ^6.0.2) and put it into your pubspec.yaml
dependencies:
flutter:
sdk: flutter
provider: ^6.0.2
- create a provider class by extending the class with
class MyModel extends ChangeNotifier{
//put your method here
}
- use the method at the place you need it with:
Provider.of<MyModel>(context,listen: false).yourMethod();
- make sure to register the provider-class in your main:
void main() {
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => MyModel()),
],
child:MyApp()),
);
}
get more information here: https://docs.flutter.dev/development/data-and-backend/state-mgmt/simple
CodePudding user response:
It happens because photoBottomSheet()
is in the PickedImage()
. Change your code like this.
class PhotoUpload {
Future pickedImage() async {
// Your code
}
photoBottomSheet() {
// Your code
}
}