方法'CreateProfile'在类型'_ProfileScreenState'中未定义。

13 浏览
0 Comments

方法'CreateProfile'在类型'_ProfileScreenState'中未定义。

我刚接触Flutter,并正在使用Flutter开发用户图像和用户详细信息更新部分。我的代码中出现了以下错误,位于ProfileScreen.dart文件中。非常感谢您的帮助。

  • 错误:在'_ProfileScreenState'类中未定义方法'CreateProfile'。

  • '_ProfileScreenState'来自'package:image_uploader/Profile/ProfileScreen.dart'('lib/Profile/ProfileScreen.dart')。

    请将名称更正为现有方法的名称,或定义一个名为'CreateProfile'的方法。

    MaterialPageRoute(builder: (context) => CreateProfile()))

    ^^^^^^^^^^^^^

  • 参数类型'Object'无法赋值给参数类型'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 {

@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: [

Text(

"点击按钮添加个人资料",

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(

"添加个人资料",

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 {

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: [

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: [

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: [

Text(

"选择个人照片",

style: TextStyle(

fontSize: 20.0,

),

),

SizedBox(

height: 20,

),

Row(mainAxisAlignment: MainAxisAlignment.center, children: [

TextButton.icon(

icon: Icon(Icons.camera),

onPressed: () {

takePhoto(ImageSource.camera);

},

label: Text("相机"),

),

TextButton.icon(

icon: Icon(Icons.image),

onPressed: () {

takePhoto(ImageSource.gallery);

},

label: Text("图库"),

),

])

],

),

);

}

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 "姓名不能为空";

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: "姓名",

helperText: "姓名不能为空",

hintText: "Dev Stack",

),

);

}

Widget professionTextField() {

return TextFormField(

//controller: _profession,

validator: (value) {

if (value!.isEmpty) return "职业不能为空";

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: "职业",

helperText: "职业不能为空",

hintText: "全栈开发人员",

),

);

}

Widget dobField() {

return TextFormField(

// controller: _dob,

validator: (value) {

if (value!.isEmpty) return "出生日期不能为空";

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: "出生日期",

helperText: "提供出生日期,格式为dd/mm/yyyy",

hintText: "01/01/2020",

),

);

}

Widget titleTextField() {

return TextFormField(

// controller: _title,

validator: (value) {

if (value!.isEmpty) return "标题不能为空";

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: "标题",

helperText: "不能为空",

hintText: "Flutter开发人员",

),

);

}

Widget aboutTextField() {

return TextFormField(

// controller: _about,

validator: (value) {

if (value!.isEmpty) return "个人简介不能为空";

return null;

},

maxLines: 4,

decoration: InputDecoration(

border: OutlineInputBorder(

borderSide: BorderSide(

color: Colors.teal,

)),

focusedBorder: OutlineInputBorder(

borderSide: BorderSide(

color: Colors.orange,

width: 2,

)),

labelText: "个人简介",

helperText: "写一些关于自己的话",

hintText: "我是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: '个人资料页面',

theme: ThemeData(

primarySwatch: Colors.blue,

),

home: ProfileScreen(),

);

}

}

0
0 Comments

在上述代码中,出现了一个问题:'CreateProfile'方法在'_ProfileScreenState'类型中没有被定义。这个问题的原因是在导航器中调用了一个未定义的方法。

要解决这个问题,我们可以尝试以下方法:

1. 确保在导航器中调用的方法名称正确,并且确保方法已经在'_ProfileScreenState'类中被定义。

2. 确保在导航器中调用的方法的参数正确,并且与被调用方法的参数一致。

3. 如果确保方法名称和参数都正确无误,但问题仍然存在,那可能是由于该方法所在的类没有正确导入。请确保已经正确导入包含该方法的类。

希望以上方法能够解决这个问题。如果问题仍然存在,请提供更多的上下文信息,以便我们能够更好地帮助您解决这个问题。

0