0

I'm new to flutter and developing user image and user detail update section using flutter. I'm getting this error in my code. in ProfileScreen.dart file. appriciate your help on this.

  • Error: The method 'CreateProfile' isn't defined for the class '_ProfileScreenState'.

  • '_ProfileScreenState' is from 'package:image_uploader/Profile/ProfileScreen.dart' ('lib/Profile/ProfileScreen.dart'). Try correcting the name to the name of an existing method, or defining a method named 'CreateProfile'. MaterialPageRoute(builder: (context) => CreateProfile())) ^^^^^^^^^^^^^

  • The argument type 'Object' can't be assigned to the parameter type 'ImageProvider?'.

backgroundImage: _imageFile == null? AssetImage("assets/pic.jpg"):FileImage(file(_imageFile.path)),

ProfileScreen.dart

import 'package:flutter/material.dart';
import 'package:image_uploader/Profile/CreateProfile.dart';

class ProfileScreen extends StatefulWidget {
  ProfileScreen({ Key? key}) : super(key: key);

  @override
  _ProfileScreenState createState() => _ProfileScreenState();
}

class _ProfileScreenState extends State<ProfileScreen> {

  @override
  Widget build(BuildContext context){
    return Scaffold(
      body: button(),
    );
  }

  Widget button() {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 70),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Text(
            "Tap to button to add profile data",
            textAlign: TextAlign.center,
            style: TextStyle(
              color: Colors.teal,
              fontSize: 15,
            ),
          ),
          SizedBox(
            height: 30,
          ),

            //button 
            Center(
              child: InkWell(

                onTap: () => {
                Navigator.push(context,
                MaterialPageRoute(builder: (context) => CreateProfile()))

                },
                child: Container(
                  height: 60,
                  width: 150,
                  decoration: BoxDecoration(
                    color: Colors.teal,
                    borderRadius: BorderRadius.circular(20),
                  ),
                  child: Center(
                    child: Text(
                      "Add Proile",
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 18,
                      ),
                    ),
                  ),
                ),
              ),
            ),
        ],
      ),
    );
  }
}

CreateProfile.dart

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:convert';
import 'package:http/http.dart' ;



class CreatProfile extends StatefulWidget {
  CreatProfile({required Key key}) : super(key: key);

  @override
  _CreatProfileState createState() => _CreatProfileState();
}

class _CreatProfileState extends State<CreatProfile> {

  bool circular = false;
  late PickedFile _imageFile;

 final ImagePicker _picker = ImagePicker();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Form(

        child: ListView(
          padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 30),
          children: <Widget>[
            imageProfile(),
            SizedBox(
              height: 20,
            ),
            nameTextField(),
            SizedBox(
              height: 20,
            ),
            professionTextField(),
            SizedBox(
              height: 20,
            ),
            dobField(),
            SizedBox(
              height: 20,
            ),
            titleTextField(),
            SizedBox(
              height: 20,
            ),
            aboutTextField(),
            SizedBox(
              height: 20,
            ),

],
    ),
    ),
    );
              }



  Widget imageProfile() {
    return Center(
      child: Stack(children: <Widget>[
        CircleAvatar(
          radius: 80.0,
          backgroundImage: _imageFile == null? AssetImage("assets/pic.jpg"):FileImage(file(_imageFile.path)),

        ),
        Positioned(
          bottom: 20.0,
          right: 20.0,
          child: InkWell(
            onTap: () {
              showModalBottomSheet(
                context: context,
                builder: ((builder) => bottomSheet()),
              );
            },
            child: Icon(
              Icons.camera_alt,
              color: Colors.teal,
              size: 28.0,
            ),
          ),
        ),
      ]),
    );
  }

  Widget bottomSheet() {
    return Container(
      height: 100.0,
      width: MediaQuery.of(context).size.width,
      margin: EdgeInsets.symmetric(
        horizontal: 20,
        vertical: 20,
      ),
      child: Column(
        children: <Widget>[
          Text(
            "Choose Profile photo",
            style: TextStyle(
              fontSize: 20.0,
            ),
          ),
          SizedBox(
            height: 20,
          ),
          Row(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
            TextButton.icon(
              icon: Icon(Icons.camera),
              onPressed: () {
                takePhoto(ImageSource.camera);
              },
              label: Text("Camera"),
            ),
            TextButton.icon(
              icon: Icon(Icons.image),
              onPressed: () {
                takePhoto(ImageSource.gallery);
              },
              label: Text("Gallery"),
            ),
          ])
        ],
      ),
    );
  }

  void takePhoto(ImageSource source) async {
    final pickedFile = await _picker.pickImage(
      source: source,
    );
    setState(() {
      _imageFile = pickedFile as PickedFile;
    });
  }

  //starting text fields

  Widget nameTextField() {
    return TextFormField(
      //controller: _name,
      validator: (value) {
        if (value!.isEmpty) return "Name can't be empty";

        return null;
      },
      decoration: InputDecoration(
        border: OutlineInputBorder(
            borderSide: BorderSide(
              color: Colors.teal,
            )),
        focusedBorder: OutlineInputBorder(
            borderSide: BorderSide(
              color: Colors.orange,
              width: 2,
            )),
        prefixIcon: Icon(
          Icons.person,
          color: Colors.green,
        ),
        labelText: "Name",
        helperText: "Name can't be empty",
        hintText: "Dev Stack",
      ),
    );
  }

  Widget professionTextField() {
    return TextFormField(
      //controller: _profession,
      validator: (value) {
        if (value!.isEmpty) return "Profession can't be empty";

        return null;
      },
      decoration: InputDecoration(
        border: OutlineInputBorder(
            borderSide: BorderSide(
              color: Colors.teal,
            )),
        focusedBorder: OutlineInputBorder(
            borderSide: BorderSide(
              color: Colors.orange,
              width: 2,
            )),
        prefixIcon: Icon(
          Icons.person,
          color: Colors.green,
        ),
        labelText: "Profession",
        helperText: "Profession can't be empty",
        hintText: "Full Stack Developer",
      ),
    );
  }

  Widget dobField() {
    return TextFormField(
     // controller: _dob,
      validator: (value) {
        if (value!.isEmpty) return "DOB can't be empty";

        return null;
      },
      decoration: InputDecoration(
        border: OutlineInputBorder(
            borderSide: BorderSide(
              color: Colors.teal,
            )),
        focusedBorder: OutlineInputBorder(
            borderSide: BorderSide(
              color: Colors.orange,
              width: 2,
            )),
        prefixIcon: Icon(
          Icons.person,
          color: Colors.green,
        ),
        labelText: "Date Of Birth",
        helperText: "Provide DOB on dd/mm/yyyy",
        hintText: "01/01/2020",
      ),
    );
  }

  Widget titleTextField() {
    return TextFormField(
     // controller: _title,
      validator: (value) {
        if (value!.isEmpty) return "Title can't be empty";

        return null;
      },
      decoration: InputDecoration(
        border: OutlineInputBorder(
            borderSide: BorderSide(
              color: Colors.teal,
            )),
        focusedBorder: OutlineInputBorder(
            borderSide: BorderSide(
              color: Colors.orange,
              width: 2,
            )),
        prefixIcon: Icon(
          Icons.person,
          color: Colors.green,
        ),
        labelText: "Title",
        helperText: "It can't be empty",
        hintText: "Flutter Developer",
      ),
    );
  }

  Widget aboutTextField() {
    return TextFormField(
     // controller: _about,
      validator: (value) {
        if (value!.isEmpty) return "About can't be empty";

        return null;
      },
      maxLines: 4,
      decoration: InputDecoration(
        border: OutlineInputBorder(
            borderSide: BorderSide(
              color: Colors.teal,
            )),
        focusedBorder: OutlineInputBorder(
            borderSide: BorderSide(
              color: Colors.orange,
              width: 2,
            )),
        labelText: "About",
        helperText: "Write about yourself",
        hintText: "I am Dev Stack",
      ),
    );
  }
}

main.dart

import 'package:flutter/material.dart';
import 'package:image_uploader/Profile/ProfileScreen.dart';


void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Profile Section',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: ProfileScreen(),

    );
  }
}
  • 1
    So from the code I see the class name is wrong spelled, which is CreatProfile and you are calling CreateProfile from material Page route. – Sagar Acharya Feb 03 '22 at 11:25
  • 1
    check the createProfile.dart class name, you will see the class name that you have given is CreatProfile – Sagar Acharya Feb 03 '22 at 11:27
  • backgroundImage: _imageFile == null ? AssetImage("assets/pic.jpg") :FileImage(file(_imageFile.path)), how to correct this line –  Feb 03 '22 at 11:35
  • what is the issue with this one. – Sagar Acharya Feb 03 '22 at 11:36
  • The argument type 'Object' can't be assigned to the parameter type 'ImageProvider?'. –  Feb 03 '22 at 11:38
  • 1
    Check this one it might work for you : https://stackoverflow.com/questions/66561177/the-argument-type-object-cant-be-assigned-to-the-parameter-type-imageprovide or you can also check this one :https://stackoverflow.com/questions/69364552/how-to-solve-the-argument-type-imageproviderobject-cant-be-assigned-to-the?rq=1 – Sagar Acharya Feb 03 '22 at 11:39
  • I have correct the CreateProfile name now it diplays this error..The named parameter 'key' is required, but there's no corresponding argument. –  Feb 03 '22 at 11:42
  • 1
    see createProfile class has a parameter key you have to provide it from material Page route CreateProfile class like CreateProfile(key: UniqueKey()) – Sagar Acharya Feb 03 '22 at 11:44
  • I'm struggling with changes can you please comment the code want to change. appreciate your help –  Feb 03 '22 at 11:52
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/241686/discussion-between-sagar-acharya-and-maharabage). – Sagar Acharya Feb 03 '22 at 11:56

1 Answers1

0

Check this out you have to do this :

 Navigator.push(context,
        MaterialPageRoute(builder: (context) => CreateProfile(
key: UniqueKey()))

Let me know if it works

Sagar Acharya
  • 3,397
  • 4
  • 12
  • 34