Hi I’m not flutter expert and I’m trying to make an edit profile screen that updates user data to Laravel database but I get this error when running my screen
(Expected a value of type ‘(0 -> vold)?’, but got one of type
‘_Future<dynamic»*)
I can’t fix it, can someone please help?
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:udemy_flutter/Screens/homescreen.dart';
import 'package:udemy_flutter/auth_services.dart';
import 'package:udemy_flutter/globals.dart';
import 'package:http/http.dart' as http;
class EditProfileUI extends StatefulWidget {
static const String screenRoute = 'EditProfileUI';
const EditProfileUI({Key? key}) : super(key: key);
@override
_EditProfileUIState createState() => _EditProfileUIState();
}
class _EditProfileUIState extends State<EditProfileUI> {
String _firstname = '';
String _lastname = '';
String _email = '';
String _password = '';
String _healthproblems = '';
String _address = '';
updatedataPressed() async {
if (_firstname.isNotEmpty && _lastname.isNotEmpty && _email.isNotEmpty && _password.isNotEmpty
&& _healthproblems.isNotEmpty && _address.isNotEmpty) {
http.Response response = await AuthServices.put(_firstname,_lastname,_email, _password, _healthproblems, _address);
Map responseMap = jsonDecode(response.body);
if (response.statusCode == 200) {
// Navigator.push(
// context,
// MaterialPageRoute(
// builder: (BuildContext context) => const HomeScreen(),
// ));
} else {
errorSnackBar(context, responseMap.values.first);
}
} else {
errorSnackBar(context, 'enter all required fields');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFF2c363b),
appBar: AppBar(
backgroundColor: const Color(0xFF2c363b),
title: const Text('Profile'),
leading: IconButton(
icon: const Icon(
Icons.arrow_back,
color: Color(0xFFffae46),
), onPressed: () {
Navigator.pushNamed(context, HomeScreen.screenRoute);
},
),
),
body: Container(
padding: const EdgeInsets.only(left: 15, top: 20, right: 15),
child: GestureDetector(
onTap: () {
FocusScope.of(context).unfocus();
},
child: ListView(
children: [
Center(
child: Stack(
children: [
Container(
width: 130,
height: 130,
decoration: BoxDecoration(
border: Border.all(width: 4, color: Colors.white),
boxShadow: [
BoxShadow(
spreadRadius: 2,
blurRadius: 10,
color: Colors.black.withOpacity(0.1)
)
],
shape: BoxShape.circle,
image: const DecorationImage(
fit: BoxFit.cover,
image: NetworkImage(
''
)
)
),
),
Positioned(
bottom: 0,
right: 0,
child: Container(
height: 40,
width: 40,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
width: 4,
color: Colors.white
),
color: Colors.amber,
),
child: const Icon(
Icons.edit,
color: Colors.white,
),
)
)
],
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Column(
children: [
const SizedBox(
height: 20,
),
TextField(
decoration: const InputDecoration(
hintText: 'Enter your First Name',
),
onChanged: (value) {
_firstname = value;
},
),
const SizedBox(
height: 20,
),
TextField(
decoration: const InputDecoration(
hintText: 'Enter your Last Name',
),
onChanged: (value) {
_lastname = value;
},
),
const SizedBox(
height: 20,
),
TextField(
decoration: const InputDecoration(
hintText: 'Enter your email',
),
onChanged: (value) {
_email = value;
},
),
const SizedBox(
height: 20,
),
TextField(
decoration: const InputDecoration(
hintText: 'Enter your password',
),
onChanged: (value) {
_password = value;
},
),
const SizedBox(
height: 30,
),
TextField(
obscureText: true,
decoration: const InputDecoration(
hintText: 'Enter your Health Problems',
),
onChanged: (value) {
_healthproblems = value;
},
),
const SizedBox(
height: 30,
),
TextField(
obscureText: true,
decoration: const InputDecoration(
hintText: 'Enter your Address',
),
onChanged: (value) {
_address = value;
},
),
const SizedBox(
height: 30,
),
ElevatedButton(
onPressed: updatedataPressed(),
style: ElevatedButton.styleFrom(
backgroundColor: const Color(0xFFffae46),
padding: const EdgeInsets.symmetric(horizontal: 50),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20))
),
child: const Text("Save", style: TextStyle(
fontSize: 15,
letterSpacing: 2,
color: Colors.white
)),
)
],
),
),
],
),
),
),
);
}
}
static Future<http.Response> put(String firstname,String lastname,String email,
String password, String healthproblems, String address) async {
Map data = {
"firstname" : firstname,
"lastname" : lastname,
"email": email,
"password": password,
"healthproblems": healthproblems,
"address": address,
};
var body = json.encode(data);
var url = Uri.parse(editprofileURL + 'auth/update data');
http.Response response = await http.put(
url,
headers: editprofileheaders,
body: body,
);
print(response.body);
return response;
}
I have tried looking for solutions online but didn’t help, like I mentioned I’m not an expert and I need it for my graduation project.
2
Answers
In your design section, exactly here :
in the elevated button "onPressed" parameters, try to make it like this :
it should works
The solution is to use a method tear-off:
I guess you called the method by accident.
The updatedataPressed method is defined as async. That means it will return a Future<SomeType>.
The onPressed callback is declared as:
You called the method by writing
updatedataPressed()
and gave the result (Future<dynamic>
) to onPressed. Instead you want to pass avoid Function()
to onPressed by writing onlyupdatedataPressed
.Edit: There are other ways to achieve the same result with different code. A few examples are
onPressed: () { updatedataPressed(); },
,onPressed: () => updatedataPressed(),
andonPressed: updatedataPressed,
. However, the last is the newest, shortest and most idiomatic way.