我仍在进行颤振训练,所以还没有足够的知识。
现在,我正尝试以列表的形式从应用程序上的Web服务器接收一系列数据,我创建了API,它起作用了,现在的想法是我有一个联系人列表,每个联系人都收到了其数据并查看了它在此数据的中间是当我单击某个联系人时我想要的联系人的ID,该ID已发送到另一个类,这是联系人页面的详细信息,因此在详细信息页面中,它将ID再次发送给API以接收有关联系人的完整数据,但是当我这样做时,它说不能接收空值,任何人都可以帮忙 第一页代码和第二页代码以及模拟器中的错误no such method error the getter length was called on null receiver: null tried calling: lenght
class topfreelancers extends StatefulWidget {
@override
_topfreelancers createState() => _topfreelancers();
}
class _topfreelancers extends State<topfreelancers> {
List data = [];
@override
void initState() {
fetchData();
super.initState();
}
void fetchData() async {
final response = await http.get('http://10.0.2.2/phpapi/getfreelancers.php');
if (response.statusCode == 200) {
setState(() {
data = json.decode(response.body);
});
}
}
@override
Widget build(BuildContext context) {
return
Column(
children: <Widget>[
Row(children: <Widget>[
SizedBox(width:15),
Text(
'Weekly Top 10 Freelancers',
textAlign: TextAlign.left,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontWeight: FontWeight.normal ,fontSize: 18),
)
],)
,SizedBox(height: 20),
Container(
height:200,
width:MediaQuery.of(context).size.width,
child:ListView.builder(
physics: BouncingScrollPhysics(),
scrollDirection: Axis.horizontal,
itemCount: data.length,
itemBuilder: (BuildContext context, int index) =>
Container(
child:
Row(children: <Widget>[
SizedBox(width:10),
Column(
children: <Widget>[
Stack(
children: <Widget>[
Column(children: <Widget>[
Stack(
children: <Widget>[
SizedBox(height:10),
CircleAvatar(
backgroundImage:
NetworkImage(data[index]['image']),
child:FlatButton(
color: Colors.transparent,
splashColor: Colors.transparent,
onPressed: (){
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => information(id:data[index]['id'])
));
}
) ,
radius: 60,
),
],
),
Row(
children: <Widget>[
SizedBox(width:5),
Container(
width: 40,
height: 20,
decoration: BoxDecoration(
color: const Color(0xffffffff),
borderRadius: BorderRadius.circular(10),
boxShadow:[BoxShadow(offset: const Offset(0,3), blurRadius: 6,color: const Color(0xff000000).withOpacity(0.16),)],
),
child:Row(children:<Widget>[
SizedBox(width:2),
Icon(Icons.star,color:Colors.yellow,size:20),
SizedBox(width:1),
Text(
data[index]['id'],
textAlign: TextAlign.center,
style: TextStyle(
fontFamily: 'SF Display',
fontSize: 11,
color: Color(0xff202020),
),
)],),
)
],
)
],)
],
),
SizedBox(height:5),
Text(
data[index]['name'],
textAlign: TextAlign.center,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontWeight: FontWeight.bold,fontSize: 15),
),
SizedBox(height:5),
Text(
data[index]['category'],
textAlign: TextAlign.center,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontWeight: FontWeight.normal,fontSize: 12),
)
],
)
],)
)
),
),
Row(
children: <Widget>[
SizedBox(width:700),
GestureDetector(onTap: (){
null;
},
child:
const Text(
'see more',
textAlign: TextAlign.center,
style: TextStyle(
fontFamily: 'SF Display',
fontSize: 16,
color: Color(0xff2a3377),
decoration: TextDecoration.underline,
),
),
)
],
)
]
);
}
}
第二页
class ApiProvider {
final String key =information().id;
Future<Post> fetchPost() async {
final response = await http.post("https://10.0.2.2/phpapi/select.php", body:{"key": key});
if (response.statusCode == 200) {
return Post.fromJson(json.decode(response.body));
} else {
throw Exception('Failed to load post');
}
}
}
class Post{
final userid;
final username;
final userphone;
Post({this.userid,this.username,this.userphone});
factory Post.fromJson(Map<String, dynamic> json){
return Post(
userid: json['id'],
username: json['name'],
userphone: json['phone'],
);
}
}
class information extends StatefulWidget {
String id;
information({this.id});
@override
_data createState() => _data();
}
class _data extends State<information> {
final Future<Post> post = ApiProvider().fetchPost();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body:
Center(
child: FutureBuilder<Post>(
future: post,
builder: (context, snapshot){
if (snapshot.hasData){
return
Column(
children: <Widget>[
SizedBox(height:80),
SizedBox(height:80),
SizedBox(height:80),
Text(snapshot.data.userid),
Text(snapshot.data.username),
Text(snapshot.data.userphone)
],
);
}else if (snapshot.hasError){
return Text("${snapshot.error}");
}
return CircularProgressIndicator();
}
)
)
);
}
}
答案 0 :(得分:1)
您可以使用initState
并使用fetchPost(widget.id)
设置参数
代码段
Future<Post> fetchPost(String key) async {
final response = await http
.post("https://10.0.2.2/phpapi/select.php", body: {"key": key});
...
class _data extends State<information> {
Future<Post> post;
@override
void initState() {
super.initState();
post = ApiProvider().fetchPost(widget.id);
}
完整代码段
class ApiProvider {
//final String key = information().id;
Future<Post> fetchPost(String key) async {
final response = await http
.post("https://10.0.2.2/phpapi/select.php", body: {"key": key});
if (response.statusCode == 200) {
return Post.fromJson(json.decode(response.body));
} else {
throw Exception('Failed to load post');
}
}
}
class information extends StatefulWidget {
String id;
information({this.id});
@override
_data createState() => _data();
}
class _data extends State<information> {
Future<Post> post;
@override
void initState() {
super.initState();
post = ApiProvider().fetchPost(widget.id);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: FutureBuilder<Post>(
future: post,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Column(
children: <Widget>[
SizedBox(height: 80),
SizedBox(height: 80),
SizedBox(height: 80),
Text(snapshot.data.userid),
Text(snapshot.data.username),
Text(snapshot.data.userphone)
],
);
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
}
return CircularProgressIndicator();
})));
}
}