单用户 api fetch, flutter
Single user api fetch, flutter
我只想 fetch/get 单个 api
数据,我试图打印来自 service
的响应我得到了所有 api
数据但是当我试图通过controller
模型变量不正确,
我已经在寻找使用 Service
获取单个 api 和使用 GetX
获取 Controller
架构的答案,但我没有找到任何合适的答案,我希望你能帮助我很重要。谢谢
我使用 http
和 GetX
the problem is here
var user = <UserModel>{}.obs;
user.value = _user;
它告诉
UserModel _user A value of type 'UserModel' can't be assigned to a
variable of type 'Set'. Try changing the type of the
variable, or casting the right-hand type to 'Set
这是代码
Api Service
class ApiService {
Future<UserModel> fetchApi(id) async {
var url = 'https://reqres.in/api/users/$id';
var response = await http.get(Uri.parse(url));
if(response.statusCode == 200){
var dataResponse = jsonDecode(response.body)['data'];
UserModel user = UserModel.fromJson(dataResponse);
return user;
} else {
throw Exception('Failed Get API');
}
}
}
Controller
class Controller extends GetxController {
var user = <UserModel>{}.obs;
Future fetchApi(id) async {
try {
var _user = await ApiService().fetchApi(id);
user.value = _user;
print(user);
} catch (e) {
print(e);
}
}
}
Model
class UserModel {
int id;
String email;
String name;
String avatar;
UserModel({
required this.id,
required this.email,
required this.name,
required this.avatar,
});
factory UserModel.fromJson(Map<String, dynamic> json) => UserModel(
id: json['id'],
email: json['email'],
name: json['first_name'] + ' ' + json['last_name'],
avatar: json['avatar'],
);
}
主要原因是您将用户定义为 {} 类型并且您需要 UserModel 类型。
你需要改变这个:
var user = <UserModel>{}.obs;
user.value = _user;
为此:
var user = UserModel().obs;
user.value = _user;
我只想 fetch/get 单个 api
数据,我试图打印来自 service
的响应我得到了所有 api
数据但是当我试图通过controller
模型变量不正确,
我已经在寻找使用 Service
获取单个 api 和使用 GetX
获取 Controller
架构的答案,但我没有找到任何合适的答案,我希望你能帮助我很重要。谢谢
我使用 http
和 GetX
the problem is here
var user = <UserModel>{}.obs;
user.value = _user;
它告诉
UserModel _user A value of type 'UserModel' can't be assigned to a variable of type 'Set'. Try changing the type of the variable, or casting the right-hand type to 'Set
这是代码
Api Service
class ApiService {
Future<UserModel> fetchApi(id) async {
var url = 'https://reqres.in/api/users/$id';
var response = await http.get(Uri.parse(url));
if(response.statusCode == 200){
var dataResponse = jsonDecode(response.body)['data'];
UserModel user = UserModel.fromJson(dataResponse);
return user;
} else {
throw Exception('Failed Get API');
}
}
}
Controller
class Controller extends GetxController {
var user = <UserModel>{}.obs;
Future fetchApi(id) async {
try {
var _user = await ApiService().fetchApi(id);
user.value = _user;
print(user);
} catch (e) {
print(e);
}
}
}
Model
class UserModel {
int id;
String email;
String name;
String avatar;
UserModel({
required this.id,
required this.email,
required this.name,
required this.avatar,
});
factory UserModel.fromJson(Map<String, dynamic> json) => UserModel(
id: json['id'],
email: json['email'],
name: json['first_name'] + ' ' + json['last_name'],
avatar: json['avatar'],
);
}
主要原因是您将用户定义为 {} 类型并且您需要 UserModel 类型。
你需要改变这个:
var user = <UserModel>{}.obs;
user.value = _user;
为此:
var user = UserModel().obs;
user.value = _user;