Json 颤振:http包-获取请求失败
我正在学习颤振中的api和http请求,并且在发出get请求时遇到了问题 在我的项目中。我想从api中获取用户详细信息。但是,当我运行它时,它显示设备中没有任何内容,任何人都可以在以下方面帮助我:这是我的示例代码: [在此处输入图像描述][2] json文件:Json 颤振:http包-获取请求失败,json,flutter,http,dart,get,Json,Flutter,Http,Dart,Get,我正在学习颤振中的api和http请求,并且在发出get请求时遇到了问题 在我的项目中。我想从api中获取用户详细信息。但是,当我运行它时,它显示设备中没有任何内容,任何人都可以在以下方面帮助我:这是我的示例代码: [在此处输入图像描述][2] json文件: [{"id":1,"nomcomplet":"firas","telephone":"20552954","datenaissa
[{"id":1,"nomcomplet":"firas","telephone":"20552954","datenaissance":"8\/01\/1998","email":"firas@gmail.com","password":"12847"}]
代码颤振镖:用户型号:
class User{
final String nomcomplet;
final String telephone;
final String datenaissance ;
final String email;
final String password;
User({this.nomcomplet, this.telephone, this.datenaissance, this.email, this.password});
factory User.fromJson(Map<String, dynamic> jsonUser) => User(
nomcomplet: jsonUser['nomcomplet'],
telephone: jsonUser['telephone'],
datenaissance: jsonUser['datenaissance'],
email: jsonUser['email'],
password: jsonUser['password'],
);
Map<String, dynamic> toJson() => {
"nomcomplet": nomcomplet,
"telephone": telephone,
"datenaissance": datenaissance,
"email": email,
"password": password,
};
}
code dart userwidget "flutter"
类用户扩展无状态小部件{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
背景颜色:主题。背景。脚手架背景颜色,
标高:0.0,
领先:IconButton(
图标:图标(
Icons.arrow_back,
颜色:颜色。蓝色,),
onPressed:(){Navigator.of(context).push(MaterialPageRoute(
生成器:(BuildContext上下文)=>Home());},
),),
正文:中(
子:SingleChildScrollView(
孩子:表格(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
正文(
“Compte”,
样式:TextStyle(fontSize:40,fontWeight:fontWeight.w500),
),
大小盒子(
身高:10,
),
未来建设者(
future:getUser(),
生成器:(上下文,快照){
if(snapshot.hasData)
if(snapshot.connectionState==connectionState.done)
如果(snapshot.data!=null){
{
返回SingleChildScrollView(
孩子:填充(
填充:常数边集全部(12.0),
子:列(
儿童:[
卡片(
子:列(
crossAxisAlignment:crossAxisAlignment.center,
儿童:[
列表砖(
标题:文本(“nomplett”),
字幕:文本(${snapshot.data.nomplett}),
),
列表砖(
标题:文本(“电话”),
字幕:文本(${snapshot.data.telephone}),
),
列表砖(
标题:文本(“诞生日期”),
字幕:文本(${snapshot.data.datenaissance}),
),
列表砖(
标题:文本(“电子邮件”),
字幕:文本(${snapshot.data.email}),
),
列表砖(
标题:文本(“通行证”),
字幕:文本(${snapshot.data.password})
,
))]))])));}
}
else if(snapshot.hasrerror){
返回文本(snapshot.error.toString());
}
返回循环ProgressIndicator();
},
),
]),
))));
}
}
//函数获取JSON数据
Future getUser()异步{
常量url=http://url/api/Users';
最终响应=等待http.get(url);
如果(response.statusCode==200){
final jsonUser=jsonDecode(response.body);
返回User.fromJson(jsonUser);
}否则{
抛出异常();
}
}
[1]: https://i.stack.imgur.com/GSh4g.jpg
[2]: https://i.stack.imgur.com/k7BUE.jpg
服务器似乎返回列表
。试试这个<代码>返回User.fromJson(jsonUser[0])代码>
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'usermodel.dart';
import 'homepage.dart';
class user extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).scaffoldBackgroundColor,
elevation: 0.0,
leading: IconButton(
icon: Icon(
Icons.arrow_back,
color: Colors.blue,),
onPressed: () { Navigator.of(context).push(MaterialPageRoute(
builder: (BuildContext context) => Home ()));},
),),
body: Center(
child: SingleChildScrollView(
child: Form(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Compte",
style: TextStyle(fontSize: 40, fontWeight: FontWeight.w500),
),
SizedBox(
height: 10,
),
FutureBuilder<User>(
future: getUser(),
builder: (context, snapshot){
if(snapshot.hasData)
if (snapshot.connectionState == ConnectionState.done)
if (snapshot.data != null) {
{
return SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Column(
children: <Widget>[
Card(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
ListTile(
title: Text("NomComplet"),
subtitle: Text(" ${snapshot.data.nomcomplet}"),
),
ListTile(
title: Text("Téléphone"),
subtitle: Text(" ${snapshot.data.telephone}"),
),
ListTile(
title: Text("Date de naissance"),
subtitle: Text(" ${snapshot.data.datenaissance}"),
),
ListTile(
title: Text("Email"),
subtitle: Text(" ${snapshot.data.email}"),
),
ListTile(
title: Text("Mot de passe"),
subtitle: Text(" ${snapshot.data.password}"
,
))]))])));}
}
else if(snapshot.hasError){
return Text(snapshot.error.toString());
}
return CircularProgressIndicator();
},
),
]),
))));
}
}
// Function to get the JSON data
Future<User> getUser() async{
const url = 'http://url/api/Users';
final response = await http.get(url);
if(response.statusCode == 200){
final jsonUser = jsonDecode(response.body);
return User.fromJson(jsonUser);
}else{
throw Exception();
}
}
[1]: https://i.stack.imgur.com/GSh4g.jpg
[2]: https://i.stack.imgur.com/k7BUE.jpg