gpt4 book ai didi

dart - 如何从flutter中的json动态设置文本值

转载 作者:行者123 更新时间:2023-12-03 04:16:49 25 4
gpt4 key购买 nike

嗨, friend 们,如何动态设置文本值,我正在使用JSON来获取数据,但是当我刷新数据时,在应用程序页面启动之前,我会在initstate处按每次加载调用JSON,对不起 friend ,我没有对抖动了解很多,所以请帮助我,请找到下面的代码

String name, userimage, birth, c_id, email, mobile_number;

class Profile extends StatefulWidget {
@override
State<StatefulWidget> createState() {
Profile_Customer profile_customer() => Profile_Customer();
return profile_customer();
}
}

class Profile_Customer extends State<Profile> {
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text('Profile'),
backgroundColor: primarycolor,
leading: new IconButton(
icon: new Icon(Icons.arrow_back),
onPressed: () {
Navigator.pushReplacement(
context,
new MaterialPageRoute(
builder: (context) => new HomeScreen()));
}),
),
body: new Builder(builder: (BuildContext context) {
return new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Container(
child: new Image.asset('assets/rural_post_logo.png',
fit: BoxFit.cover),
margin: EdgeInsets.only(bottom: 15.0),
),
new Container(
child: new CircleAvatar(
child: new Image.network(userimage,
width: 100.0, height: 100.0, fit: BoxFit.cover),
),
margin: EdgeInsets.only(top: 10.0),
alignment: Alignment(0.0, 0.0),
),
new Container(
child: new Text(name),
margin: EdgeInsets.only(top: 10.0),
),
new Container(
child: new Divider(
height: 2.0,
color: primarycolor,
),
margin: EdgeInsets.only(right: 10.0, left: 10.0, top: 10.0),
),
new Container(
child: new Text(
'Birth Date',
style: new TextStyle(
fontWeight: FontWeight.bold, fontSize: 16.0),
),
alignment: Alignment(-1.0, 0.0),
margin: EdgeInsets.only(top: 10.0, left: 10.0),
),
new Container(
child: new Text(birth),
alignment: Alignment(-1.0, 0.0),
margin: EdgeInsets.only(top: 10.0, left: 10.0),
),
new Container(
child: new Divider(
height: 2.0,
color: primarycolor,
),
margin: EdgeInsets.only(right: 10.0, left: 10.0, top: 10.0),
),
new Container(
child: new Text(
'Customer ID',
style: new TextStyle(
fontWeight: FontWeight.bold, fontSize: 16.0),
),
alignment: Alignment(-1.0, 0.0),
margin: EdgeInsets.only(top: 10.0, left: 10.0),
),
new Container(
child: new Text(c_id),
alignment: Alignment(-1.0, 0.0),
margin: EdgeInsets.only(top: 10.0, left: 10.0),
),
new Container(
child: new Divider(
height: 2.0,
color: primarycolor,
),
margin: EdgeInsets.only(right: 10.0, left: 10.0, top: 10.0),
),
new Container(
child: new Text(
'Email',
style: new TextStyle(
fontWeight: FontWeight.bold, fontSize: 16.0),
),
alignment: Alignment(-1.0, 0.0),
margin: EdgeInsets.only(top: 10.0, left: 10.0),
),
new Container(
child: new Text(email),
alignment: Alignment(-1.0, 0.0),
margin: EdgeInsets.only(top: 10.0, left: 10.0),
),
new Container(
child: new Divider(
height: 2.0,
color: primarycolor,
),
margin: EdgeInsets.only(right: 10.0, left: 10.0, top: 10.0),
),
new Container(
child: new Text(
'Mobile number',
style: new TextStyle(
fontWeight: FontWeight.bold, fontSize: 16.0),
),
alignment: Alignment(-1.0, 0.0),
margin: EdgeInsets.only(top: 10.0, left: 10.0),
),
new Container(
child: new Text(mobile_number),
alignment: Alignment(-1.0, 0.0),
margin: EdgeInsets.only(top: 10.0, left: 10.0),
),
new Container(
child: new RaisedButton(
onPressed: () {
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => new HomeScreen()));
},
color: secondarycolor,
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(5.0)),
child: new Text('Update Profile',
style: new TextStyle(color: Colors.white)),
),
width: 300.0,
height: 40.0,
margin: EdgeInsets.only(top: 10.0),
)
],
);
}),
),
);
}

@override
void initState() {
super.initState();
profilejson();
}
}

void profilejson() async {
SharedPreferences pref = await SharedPreferences.getInstance();
var profile_url = url + "view_profile&userid=" + pref.getString('userid');
print(profile_url);
http.Response profileresponse = await http.get(profile_url);
var profile_response_json = json.decode(profileresponse.body);
name = profile_response_json['username'];
userimage = profile_response_json['image'];
birth = profile_response_json['dob'];
c_id = profile_response_json['customerid'];
email = profile_response_json['email'];
mobile_number = profile_response_json['phone'];
}

最佳答案

您可以使用StatefulWidgetsetState完成此操作,以随时更改布局。由于您的代码中已经包含小部件,因此在设置变量时只需调用setState。而且profilejson()也应该在状态内:

...
profilejson() async {
SharedPreferences pref = await SharedPreferences.getInstance();
var profile_url = url + "view_profile&userid=" + pref.getString('userid');
print(profile_url);
http.Response profileresponse = await http.get(profile_url);
var profile_response_json = json.decode(profileresponse.body);

// the variables you want the layout to be updated with
setState(() {
name = profile_response_json['username'];
userimage = profile_response_json['image'];
birth = profile_response_json['dob'];
c_id = profile_response_json['customerid'];
email = profile_response_json['email'];
mobile_number = profile_response_json['phone'];
})
}

@override
void initState() {
super.initState();
profilejson();
}
...

完整代码:
String name, userimage, birth, c_id, email, mobile_number;

class Profile extends StatefulWidget {
@override
State<StatefulWidget> createState() {
Profile_Customer profile_customer() => Profile_Customer();
return profile_customer();
}
}

class Profile_Customer extends State<Profile> {
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text('Profile'),
backgroundColor: primarycolor,
leading: new IconButton(
icon: new Icon(Icons.arrow_back),
onPressed: () {
Navigator.pushReplacement(
context,
new MaterialPageRoute(
builder: (context) => new HomeScreen()));
}),
),
body: email != null ? new Builder(builder: (BuildContext context) {
return new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Container(
child: new Image.asset('assets/rural_post_logo.png',
fit: BoxFit.cover),
margin: EdgeInsets.only(bottom: 15.0),
),
new Container(
child: new CircleAvatar(
child: new Image.network(userimage,
width: 100.0, height: 100.0, fit: BoxFit.cover),
),
margin: EdgeInsets.only(top: 10.0),
alignment: Alignment(0.0, 0.0),
),
new Container(
child: new Text(name),
margin: EdgeInsets.only(top: 10.0),
),
new Container(
child: new Divider(
height: 2.0,
color: primarycolor,
),
margin: EdgeInsets.only(right: 10.0, left: 10.0, top: 10.0),
),
new Container(
child: new Text(
'Birth Date',
style: new TextStyle(
fontWeight: FontWeight.bold, fontSize: 16.0),
),
alignment: Alignment(-1.0, 0.0),
margin: EdgeInsets.only(top: 10.0, left: 10.0),
),
new Container(
child: new Text(birth),
alignment: Alignment(-1.0, 0.0),
margin: EdgeInsets.only(top: 10.0, left: 10.0),
),
new Container(
child: new Divider(
height: 2.0,
color: primarycolor,
),
margin: EdgeInsets.only(right: 10.0, left: 10.0, top: 10.0),
),
new Container(
child: new Text(
'Customer ID',
style: new TextStyle(
fontWeight: FontWeight.bold, fontSize: 16.0),
),
alignment: Alignment(-1.0, 0.0),
margin: EdgeInsets.only(top: 10.0, left: 10.0),
),
new Container(
child: new Text(c_id),
alignment: Alignment(-1.0, 0.0),
margin: EdgeInsets.only(top: 10.0, left: 10.0),
),
new Container(
child: new Divider(
height: 2.0,
color: primarycolor,
),
margin: EdgeInsets.only(right: 10.0, left: 10.0, top: 10.0),
),
new Container(
child: new Text(
'Email',
style: new TextStyle(
fontWeight: FontWeight.bold, fontSize: 16.0),
),
alignment: Alignment(-1.0, 0.0),
margin: EdgeInsets.only(top: 10.0, left: 10.0),
),
new Container(
child: new Text(email),
alignment: Alignment(-1.0, 0.0),
margin: EdgeInsets.only(top: 10.0, left: 10.0),
),
new Container(
child: new Divider(
height: 2.0,
color: primarycolor,
),
margin: EdgeInsets.only(right: 10.0, left: 10.0, top: 10.0),
),
new Container(
child: new Text(
'Mobile number',
style: new TextStyle(
fontWeight: FontWeight.bold, fontSize: 16.0),
),
alignment: Alignment(-1.0, 0.0),
margin: EdgeInsets.only(top: 10.0, left: 10.0),
),
new Container(
child: new Text(mobile_number),
alignment: Alignment(-1.0, 0.0),
margin: EdgeInsets.only(top: 10.0, left: 10.0),
),
new Container(
child: new RaisedButton(
onPressed: () {
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => new HomeScreen()));
},
color: secondarycolor,
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(5.0)),
child: new Text('Update Profile',
style: new TextStyle(color: Colors.white)),
),
width: 300.0,
height: 40.0,
margin: EdgeInsets.only(top: 10.0),
)
],
);
}) : new Center(child: new CircularProgressIndicator()),
),
);
}

profilejson() async {
SharedPreferences pref = await SharedPreferences.getInstance();
var profile_url = url + "view_profile&userid=" + pref.getString('userid');
print(profile_url);
http.Response profileresponse = await http.get(profile_url);
var profile_response_json = json.decode(profileresponse.body);

// the variables you want the layout to be updated with
setState(() {
name = profile_response_json['username'];
userimage = profile_response_json['image'];
birth = profile_response_json['dob'];
c_id = profile_response_json['customerid'];
email = profile_response_json['email'];
mobile_number = profile_response_json['phone'];
})
}

@override
void initState() {
super.initState();
profilejson();
}
}

关于dart - 如何从flutter中的json动态设置文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51061412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com