【发布时间】:2020-05-12 22:33:17
【问题描述】:
如何正确地将返回值分配给Future<Either<Error, User>>>?因为我总是得到:
_TypeError (type 'User' is not a subtype of type 'FutureOr<Either<Error, User>>') as required by the closure's context'
我什至给出了一个相同类型的返回值。
import 'package:dartz/dartz.dart';
class APIPenggunaHelper {
final api=APIPengguna();
Future<Either<Error,User>> ambildatauser() async{
final hasilapi=await api.ambilSemuaData();
return hasilapi.fold((l){
// handling error
if(hasilapi.length()<1) return Left(MasalahDataKosong());
return Left(Masalah400());
}, (r){
// handling result
print(r.toString());
final user_= User.fromJson(jsonDecode(r));
return Right(user);
});
}
}
上面的代码应该返回 user 这是一个User class 应该是,但它显示subtype error 我上面提到的。
这是print(r.toString())的输出
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
},
.
.
.
]
编辑:
API.dart
class APIPengguna {
final String linkpengguna='/users';
Future<Either<Exception, dynamic>> ambilSemuaData() async{
try {
final mapbodi={
"id":1,
"username":"Bret"
};
// final uriapi=Uri.https('https://jsonplaceholder.typicode.com',linkpengguna);
final uriapi='https://jsonplaceholder.typicode.com'+linkpengguna;
Response respon=await get(uriapi);
var status=respon.statusCode;
var data=respon.body;
// print('data: $data');
if (status<200 || status>400) {
throw new Exception('gagal ambil data');
} else {
return Right(data); // data is a dynamic
}
} catch (e) {
return (Left(Exception('gagal ambil data, eror: $e')));
}
}
}
list_user.dart
import 'package:appsa/aplikasi/perpustakaan.dart';
class DaftarUser{
final List<User> pengguna;
DaftarUser({this.pengguna});
factory DaftarUser.fromJson(List<dynamic> jsonuser){
List pengguna_=new List();
pengguna_=jsonuser.map((p)=>User.fromJson(p)).toList();
return new DaftarUser(
pengguna:pengguna_
);
}
}
model_User.dart
import 'dart:convert';
class User {
final int id;
final String name;
final String username;
final String email;
final Address address;
final String phone;
final String website;
final Company company;
User({
this.id,
this.name,
this.username,
this.email,
this.address,
this.phone,
this.website,
this.company,
});
Map<String, dynamic> toMap() {
return {
'id': id,
'name': name,
'username': username,
'email': email,
'address': address?.toMap(),
'phone': phone,
'website': website,
'company': company?.toMap(),
};
}
static User fromMap(Map<String, dynamic> map) {
if (map == null) return null;
return User(
id: map['id']?.toInt(),
name: map['name'],
username: map['username'],
email: map['email'],
address: Address.fromMap(map['address']),
phone: map['phone'],
website: map['website'],
company: Company.fromMap(map['company']),
);
}
String toJson() => jsonEncode(toMap());
static User fromJson(String source) => fromMap(jsonDecode(source));
@override
String toString() {
return 'User(id: $id, name: $name, username: $username, email: $email, address: $address, phone: $phone, website: $website, company: $company)';
}
【问题讨论】: