- 我试图通过传递不记名令牌从api获取数据,但它抛出了这个异常。下面是我的代码:**
这是我的afterLoginResponse. dart类-
class AfterLoginResponse {
LoginData? data;
bool? operator;
bool? success;
AfterLoginResponse({
required this.operator,
required this.success,
required this.data,
});
AfterLoginResponse.fromJson(Map<String, dynamic> json) {
operator = json['operator'];
success = json['success'];
data = LoginData.fromJson(json["data"]);
}
Map<String, dynamic> toJson() => {
"operator": operator,
"success": success,
"data": data!.toJson(),
};
}
下面是我的loginData.dart类-〉
class LoginData {
String? name;
String? ext;
String? dName;
String? password;
LoginData({
required this.name,
required this.ext,
required this.dName,
required this.password,
});
factory LoginData.fromJson(Map<String, dynamic> json) {
return LoginData(
name: json['name'],
ext: json['ext'],
dName: json['d_Name'],
password : json['password'],
);
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['name'] = name;
data['ext'] = ext;
data['d_Name'] = dName;
data['password'] = password;
return data;
}
}
这是我的飞行服务 dart 课
class ApiService{
String? token;
var response1;
var response2;
Future<LoginResponseData> loginApiCall(Map<String, dynamic> param) async{
var url = Uri.parse(ApiUrls.login_url);
response1 = await http.post(url, body: param);
final data = jsonDecode(response1.body);
token = LoginResponseData.fromJson(json.decode(response1.body)).token;
return LoginResponseData(user: data['user'], token:
data['token'],error:data['error']);
}
Future<AfterLoginResponse> postLoginApiCall() async{
var afterLoginUrl = Uri.parse(ApiUrls.postLogin_url);
response2 = await http.get(afterLoginUrl , headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer
${LoginResponseData.fromJson(json.decode(response1.body)).token}'
});
if(response2.statusCode == 200){
var data = jsonDecode(response2.body);
return AfterLoginResponse.fromJson(data);
}else{
throw Exception('Failed to load data');
}
}
}
这是我的用户界面-
class HomePage extends StatefulWidget {
String email, password;
HomePage({Key? key, required this.email, required this.password})
: super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
late Future<AfterLoginResponse> afterLoginResponse;
final apiService = ApiService();
callPostLoginApi(){
return FutureBuilder<AfterLoginResponse>(
future: afterLoginResponse,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(snapshot.data!);
print(snapshot.data);
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
// By default, show a loading spinner.
return const CircularProgressIndicator();
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Home page'),
centerTitle: true,
),
body: Center(
child: Column(
children: [
ElevatedButton(
onPressed: callPostLoginApi,
child: const Text('Click me'))
],
),
)
);
}
上述代码抛出以下异常-未处理异常:无此类方法错误:对null调用了getter "body"。接收器:零E/Flutter(28599):尝试呼叫:主体E/Flutter(28599):#0对象。noSuchMethod(dart:核心补丁/对象补丁。dart:38:5)...
2条答案
按热度按时间fnx2tebb1#
在postLoginApiCall中,您正在使用response1变量调用body,该变量当时为空,导致问题更改为response2,如下所示:
aurhwmvo2#
您应该在从
loginApiCall
获得响应后调用postLoginApiCall