我的代码有问题,我有一个带有 http 请求的函数来检索用户的数据并显示它,但每次调用它时,我的应用程序都会暂停,并且此消息:发送 HTTP POST 请求将给定的标头和正文添加到给定的 URL。
[body] 设置请求的正文。它可以是 [String]、[List] 或 [Map
如果 [body] 是一个列表,则将其用作请求正文的字节列表。
但是我在代码中的其他地方使用了类似的函数并且运行良好,我不知道发生了什么
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
class Navbar extends StatefulWidget {
const Navbar({
super.key,
required this.mail,
required this.password,
});
final String mail;
final String password;
@override
State<Navbar> createState() => _NavbarState();
}
class _NavbarState extends State<Navbar> {
String left = '';
String amail = "";
String apseudo = "";
//login() is the function that paused the app anytime i call it
Future<void> login() async {
var url = Uri.parse('https://konamicash.com/solde_app');
var response = await http.post(url, headers: {
"Accept": "application/json",
"Access-Control-Allow-Origin": "*"
}, body: {
"adresse_mail": '[email protected]',
});
var reponse = jsonDecode(response.body);
print(reponse);
setState(() {
left = reponse['account'];
apseudo = reponse['pseudo'];
amail = reponse['mail'];
});
}
@override
Widget build(BuildContext context) {
login();
return Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: [
UserAccountsDrawerHeader(
accountName: Text(apseudo),
accountEmail: Text(amail),
currentAccountPicture: CircleAvatar(
child:
ClipOval(child: Image.asset("assets/images/capture-arene.png")),
),
decoration: const BoxDecoration(
color: Colors.redAccent,
// image: DecorationImage(
// image: AssetImage('assets/images/capture-arene.png'),
// fit: BoxFit.cover)
),
//currentAccountPicture: , cicle)
),
],
),
);
您应该在 initState 中调用登录函数。
@override
void initState() {
super.initState();
login();
}