gpt4 book ai didi

flutter - 分析使所需的构造函数出错

转载 作者:行者123 更新时间:2023-12-03 03:00:37 25 4
gpt4 key购买 nike

'variable'是最终值,在声明时为其指定了值,因此无法将其设置为新值。请尝试删除其中一个初始化。
这是不断出现的错误。如何删除该错误?我需要一个http发布请求。

  Future<User> fetchUser() async {
final response =
await http.get('https://coronavirus-med.herokuapp.com/api/v1/auth/register');

if (response.statusCode == 200) {
// If the server did return a 200 OK response,
// then parse the JSON.
return User.fromJson(json.decode(response.body));
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
throw Exception('Failed to load request');
}
}

class User{
final String name = ' ';
final String email = ' ';
final String password = ' ';
final String passwordConfirm = ' ';
final String role = ' ';

User({this.name, this.email, this.password, this.passwordConfirm, this.role});

factory User.fromJson(Map<String, dynamic> json){
return User(
name: json['name'],
email: json['email'],
password: json['password'],
passwordConfirm: json['passwordConfirm'],
role: json['role'],
);
}
}

最佳答案

像这样更改您的User

class User {
final String name;
final String email;
final String password;
final String passwordConfirm;
final String role;

User({
String name,
String email,
String password,
String passwordConfirm,
String role,
}) : this.name = name ?? ' ',
this.email = email ?? ' ',
this.password = password ?? ' ',
this.passwordConfirm = passwordConfirm ?? ' ',
this.role = role ?? ' ';

factory User.fromJson(Map<String, dynamic> json) {
return User(
name: json['name'],
email: json['email'],
password: json['password'],
passwordConfirm: json['passwordConfirm'],
role: json['role'],
);
}
}

关于flutter - 分析使所需的构造函数出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60820920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com