Flutter 无法在有状态小部件中使用参数
我是Flatter的初学者,我有一个api,我使用Navigator将数据传递给这个路由,它工作得很好,但只有在我调用build方法中的参数时,build方法之外的参数才能被识别,我的目标是使用build方法之外的参数来创建一个使用id参数的api调用。问题在于getClubs方法和InitState方法 这是我试过的Flutter 无法在有状态小部件中使用参数,flutter,dart,Flutter,Dart,我是Flatter的初学者,我有一个api,我使用Navigator将数据传递给这个路由,它工作得很好,但只有在我调用build方法中的参数时,build方法之外的参数才能被识别,我的目标是使用build方法之外的参数来创建一个使用id参数的api调用。问题在于getClubs方法和InitState方法 这是我试过的 import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; impor
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'club_arguments.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
class ClubDetails extends StatefulWidget {
final int id;
final String name;
final String image;
final String rank;
final String nationality;
final String stadium;
final String manager;
final String wins;
final String draws;
final String losses;
final int goals;
final int goalsIn;
ClubDetails({ this.id, this.name,this.image,this.rank,this.nationality,this.stadium,this.manager,this.wins,this.draws,this.losses,this.goals,this.goalsIn});
@override
_ClubDetailsState createState() => _ClubDetailsState();
}
class _ClubDetailsState extends State<ClubDetails> {
String stadium;
String founded;
getClub(int id) async {
http.Response response = await http.get(
'http://api.football-data.org/v2/teams/$id',
headers: {'X-Auth-Token': '86014f6025ae430dba078acc94bb2647'});
String body = response.body;
Map data = jsonDecode(body);
stadium = data['name'];
//Club(this.id,this.name,this.image,this.rank,this.nationality,this.stadium,this.manager,this.wins,this.draws,this.losses,this.goals,this.goalsIn);
setState(() {
//print(data[0]['venue']);
print(stadium);
});
}
@override
void initState() {
super.initState();
getClub(widget.id);
print(widget.id);
}
@override
Widget build(BuildContext context) {
List<Titles> titles = [
Titles("LA LIGA", "Spain","Barcelona","2008" ),
];
return Scaffold(
appBar: AppBar(
title: Text("Club Info"),
backgroundColor: Colors.blue[300],
elevation: 0.0,
),
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.centerLeft,
end: Alignment.centerRight,
colors: [Colors.purple, Colors.blue])
),
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Colors.purple, Colors.black38])),
child: ListView(
children: [
SizedBox(
height: 20,
),
Container(
width: double.infinity,
child: Card(
elevation: 4.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
child: Padding(
padding: const EdgeInsets.all(16.0),
child:
Row(
children: <Widget>[
Container(
height: 40,
width: 40,
child:
SvgPicture.network(widget.image,
),
),
const SizedBox(width:10.0),
Spacer(),
Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget> [
Text(widget.name, style: TextStyle( fontWeight:FontWeight.bold,
fontSize: 18.0,
)),
const SizedBox(height: 5.0, ),
Text("rank : "+widget.rank, style: TextStyle( fontWeight:FontWeight.bold,
fontSize: 18.0,
)),
const SizedBox(height: 5.0, ),
Text("Found : "+widget.manager, style: TextStyle( fontWeight:FontWeight.bold,
fontSize: 18.0,
)),
const SizedBox(height: 5.0, ),
Text("Nationality : "+widget.nationality, style: TextStyle( fontWeight:FontWeight.bold,
fontSize: 18.0, color: Colors.grey[600],
)),
const SizedBox(height: 5.0, ),
Text("Stadium : "+widget.stadium, style: TextStyle( fontWeight:FontWeight.bold,
fontSize: 18.0, color: Colors.grey[600],
)),
],
),
],
),
),
),
),
SizedBox(
height: 10,
),
Container(
margin: EdgeInsets.fromLTRB(10, 0, 0, 10),
child: Text(
"Achieved Titles",
style: TextStyle(fontSize: 17, fontWeight: FontWeight.w900, color: Colors.white),
),
),
SizedBox(
height: 10,
),
Container(
margin: EdgeInsets.fromLTRB(5, 0, 5, 0),
child: ListView.builder(
shrinkWrap: true,
physics : NeverScrollableScrollPhysics(),
itemBuilder: (context, index){
return Card(
elevation: 4.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
child: Padding(
padding: const EdgeInsets.all(16.0),
child:
Row(
children: <Widget>[
CircleAvatar(
backgroundImage: NetworkImage("https://www.impacttrophies.co.uk/content/images/thumbs/0065685_tower-football-trophy-gold.jpeg"),
),
const SizedBox(width:10.0),
Spacer(),
Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget> [
Text(titles[index].name, style: TextStyle( fontWeight:FontWeight.bold,
fontSize: 18.0,
)),
const SizedBox(height: 5.0, ),
Text("country : "+titles[index].country, style: TextStyle( fontWeight:FontWeight.bold,
fontSize: 18.0, color: Colors.grey[600],
)),
const SizedBox(height: 5.0, ),
Text("club : "+titles[index].club, style: TextStyle( fontWeight:FontWeight.bold,
fontSize: 18.0, color: Colors.grey[600],
)),
const SizedBox(height: 5.0, ),
Text("year : "+titles[index].year, style: TextStyle( fontWeight:FontWeight.bold,
fontSize: 18.0, color: Colors.grey[600],
)),
],
),
],
),
),
);
},
itemCount: titles.length,
),
),
SizedBox(
height: 70,
),
],
),
)
),
);
}
}
class Stats{
String title;
String result;
Stats(this.title,this.result);
}
class Team {
String name;
String image;
String date;
Team(this.name,this.image,this.date);
}
class Titles {
String name;
String country;
String club;
String year;
Titles(this.name,this.country,this.club,this.year);
}
class Club {
int id;
String name;
String image;
String rank;
String nationality;
String stadium;
String manager;
String wins;
String draws;
String losses;
int goals;
int goalsIn;
Club(this.id, this.name, this.image, this.rank, this.nationality,
this.stadium, this.manager, this.wins, this.draws, this.losses,
this.goals, this.goalsIn);
}
import'包装:flift/cupertino.dart';
进口“包装:颤振/材料.省道”;
导入“package:flatter_svg/svg.dart”;
导入“club_arguments.dart”;
将“package:http/http.dart”导入为http;
导入“dart:convert”;
类ClubDetails扩展了StatefulWidget{
最终int id;
最后的字符串名;
最终字符串图像;
最后的字符串秩;
最终国籍;
最后的弦乐场;
最终字符串管理器;
最后的弦乐获胜;
最后的字符串绘制;
最终管柱损失;
最终目标;
最终目标;
俱乐部详情({this.id,this.name,this.image,this.rank,this.national,this.stadium,this.manager,this.wins,this.draw,this.loss,this.goalsIn});
@凌驾
_ClubDetailsState createState()=>\u ClubDetailsState();
}
类_clubdetailssState扩展状态{
弦球场;
字符串创建;
getClub(内部id)异步{
http.Response-Response=等待http.get(
'http://api.football-data.org/v2/teams/$id',
标题:{'X-Auth-Token':'86014f6025ae430dba078acc94bb2647'});
字符串体=response.body;
地图数据=jsonDecode(主体);
体育场=数据['name'];
//俱乐部(这个。id,这个。姓名,这个。形象,这个。等级,这个。国籍,这个。体育场,这个。经理,这个。赢,这个。平局,这个。输,这个。进球,这个。进球);
设置状态(){
//打印(数据[0][“地点]);
印刷品(体育场);
});
}
@凌驾
void initState(){
super.initState();
getClub(widget.id);
打印(widget.id);
}
@凌驾
小部件构建(构建上下文){
列表标题=[
标题(“西甲”、“西班牙”、“巴塞罗那”、“2008”),
];
返回脚手架(
appBar:appBar(
标题:文本(“俱乐部信息”),
背景颜色:颜色。蓝色[300],
标高:0.0,
),
主体:容器(
装饰:盒子装饰(
梯度:线性梯度(
开始:Alignment.centerLeft,
结束:对齐。中间右侧,
颜色:[颜色。紫色,颜色。蓝色])
),
子:容器(
装饰:盒子装饰(
梯度:线性梯度(
开始:Alignment.topLeft,
结束:对齐。右下角,
颜色:[colors.purple,colors.black38]),
子:ListView(
儿童:[
大小盒子(
身高:20,
),
容器(
宽度:double.infinity,
孩子:卡片(
标高:4.0,
形状:圆形矩形边框(
边界半径:边界半径。圆形(10.0),
),
孩子:填充(
填充:常数边集全部(16.0),
儿童:
划船(
儿童:[
容器(
身高:40,
宽度:40,
儿童:
SvgPicture.network(widget.image,
),
),
常量大小框(宽度:10.0),
垫片(),
纵队(
crossAxisAlignment:crossAxisAlignment.end,
儿童:[
Text(widget.name,style:TextStyle(fontwweight:fontwweight.bold,
字体大小:18.0,
)),
const SizedBox(高度:5.0,),
Text(“rank:+widget.rank,style:TextStyle(fontWeight:fontWeight.bold,
字体大小:18.0,
)),
const SizedBox(高度:5.0,),
Text(“找到:”+widget.manager,样式:TextStyle(fontWeight:fontWeight.bold,
字体大小:18.0,
)),
const SizedBox(高度:5.0,),
文本(“国籍:”+widget.national,样式:TextStyle(fontWeight:fontWeight.bold,
fontSize:18.0,颜色:Colors.灰色[600],
)),
const SizedBox(高度:5.0,),
Text(“Stadium:+widget.Stadium,style:TextStyle(fontWeight:fontWeight.bold,
fontSize:18.0,颜色:Colors.灰色[600],
)),
],
),
],
),
),
),
),
大小盒子(
身高:10,
),
容器(
边距:从LTRB(10,0,0,10)开始的边距集,
子:文本(
“获得的头衔”,
样式:TextStyle(fontSize:17,fontWeight:fontWeight.w900,颜色:Colors.white),
),
),
大小盒子(
身高:10,
),
容器(
边距:从LTRB(5,0,5,0)开始的边距集,
子项:ListView.builder(
收缩膜:对,
物理学:NeverscrollableScroll物理学(),
物品制造商:(公司)
class ClubDetails extends StatefulWidget {
final int id ;
ClubDetails({this.id});
@override
_ClubDetailsState createState() => _ClubDetailsState();
}
@override
void initState() {
super.initState();
getClub(widget.id);
}
ClubDetails(id : 1);
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Issue66934490(name: 'name');
}
}
class Issue66934490 extends StatefulWidget {
final String name;
Issue66934490({
this.name,
});
@override
_Issue66934490State createState() => _Issue66934490State();
}
class _Issue66934490State extends State<Issue66934490> {
@override
Widget build(BuildContext context) {
return Container(
child: Text(widget.name),
);
}
}