问题描述
我有页面导航到具有变量(dID)的第二页,等待它已被更改的返回相同的值,在第二页上,如果我不执行任何操作,则如果更改了滑块,该值必须返回而没有任何更改(dID)的值必须增加(1),但是当我Navigate.pop时,返回值相同而没有增加。
新更新:经过多次测试,我发现问题出在第二页,当我更改滑块值时,我调用一个函数以获取新值(newSavedDayId),并且可以正确获取它,但仅在函数内部,更改滑块并获取新值后,它会返回null:
newSavedDayId = Instance of 'Future<dynamic>'
第二页返回值:
import 'package:Flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import '../classes/viewdigree.dart';
import 'package:youtube_player_Flutter/youtube_player_Flutter.dart';
import 'package:shared_preferences/shared_preferences.dart';
class Days extends StatefulWidget {
@override
_Daysstate createState() => _Daysstate();
}
TextEditingController insertedDegree = TextEditingController();
class _Daysstate extends State<Days> {
@override
Widget build(BuildContext context) {
List digrees = [];
var widthView = MediaQuery.of(context).size.width;
Map rdata = ModalRoute.of(context).settings.arguments;
var did = int.parse(rdata['dayId'].toString());
var u_id = rdata['userID'];
var m_id = rdata['courseId'];
var lang = rdata['lang'];
int w_id;
var newSavedDayId = rdata['savedDayID']; ----// received value
var username;
if (did <= 6) {
w_id = 1;
} else if (did <= 12) {
w_id = 2;
} else if (did <= 18) {
w_id = 3;
} else {
w_id = 4;
}
Future<List> fetchDigrees() async {
var url =
'https://srdtraining.com/api/controller/activities/activiy_list.PHP?d_id=$did&m_id=$m_id&u_id=$u_id';
var response = await http.get(url);
var data = jsonDecode(response.body);
print(data);
for (var x in data) {
Digree newdigree = Digree(
x['index'],x['title_k'],x['title_a'],x['aya'],x['link'],x['activity_k'],x['activity_a'],x['act_id'],x['mydigree']);
digrees.add(newdigree);
}
return digrees;
}
// Insert Func
send_degree(uId,actId,degree,w_id,did,m_id) async {
var sendData = {
'u_id': uId.toString(),'act_id': actId.toString(),'digree': degree.toString(),'m_id': m_id.toString(),'d_id': did.toString(),'w_id': w_id.toString()
};
var url = 'https://srdtraining.com/api/controller/data/changedata.PHP';
var response = await http.post(url,body: sendData);
}
// End of Insert Func
//get user status .................. // this is the function to get new value
void getUserStatus() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
setState(() {
username = preferences.getString('username');
});
var url =
"http://srdtraining.com/api/controller/users/status_user.PHP?username=$username";
var response = await http.get(url);
var data = jsonDecode(response.body);
setState(() {
newSavedDayId = int.parse(data['d_id']); ----// Set New value by getting it from API after increasing by server.
});
print('newSavedDayId = $newSavedDayId');----// here it is print new value with increasing correctly
}
// End get user
return FutureBuilder(
future: fetchDigrees(),builder: (context,snapshot) {
if (snapshot.data == null) {
return Scaffold(
body: Container(
decoration: Boxdecoration(
image: decorationImage(
image: Assetimage('./assets/images/background.png'),fit: BoxFit.cover)),child: Center(
child: Text("Loading"),),);
} else {
YoutubePlayerController _controller = YoutubePlayerController(
initialVideoId: snapshot.data[0].link,flags: YoutubePlayerFlags(
autoplay: false,mute: false,));
return Scaffold(
appBar: AppBar(
leading: IconButton(
icon: Icon(Icons.arrow_back),onpressed: () {
Navigator.pop(context,newSavedDayId);
}),backgroundColor: Colors.pink[900],title: Text(
'ژیان و قورئان',style: TextStyle(fontSize: 32),centerTitle: true,body: Container(
decoration: Boxdecoration(
image: decorationImage(
image: Assetimage('./assets/images/background.png'),height: MediaQuery.of(context).size.height,width: MediaQuery.of(context).size.width,child: ListView(
padding: EdgeInsets.fromLTRB(25,20,25,20),shrinkWrap: true,children: <Widget>[
Text(
lang == 'k'
? snapshot.data[0].title_k
: snapshot.data[0].title_a,textAlign: TextAlign.center,style: TextStyle(fontSize: 32,color: Colors.white)),//for top margin
SizedBox(height: 20.0),// dexription
Container(
padding: const EdgeInsets.all(15),width: widthView,decoration: Boxdecoration(
borderRadius: BorderRadius.circular(25),color: const Color.fromrGBO(180,80,0.3)),child: Text(snapshot.data[0].aya,textAlign: TextAlign.justify,textDirection: TextDirection.rtl,style: TextStyle(
fontFamily: 'Hafs',fontSize: 26,color: Colors.greenAccent[100])),// Now populating your ListView for `Directionality`
Column(
children: <Widget>[
// Start activities
Column(
children: snapshot.data.map<Widget>((item) {
double _value =
double.parse(item.mydigree.toString());
return Directionality(
textDirection: TextDirection.rtl,child: Column(
children: <Widget>[
Container(
decoration: Boxdecoration(
borderRadius: BorderRadius.circular(15),border: Border.all(
width: 2,color: Colors.white),color:
Color.fromrGBO(230,200,0.2)),padding: EdgeInsets.all(25),margin: EdgeInsets.fromLTRB(0,25),child: Column(
children: <Widget>[
Text(
lang == 'k'
? item.activity_k
: item.activity_a,style: TextStyle(
fontSize: 28,SizedBox(
height: 15,Container(
decoration: Boxdecoration(
BoxShadow: [
BoxShadow(
color: Colors.yellow[200]
.withOpacity(0.2),spreadRadius: 2,blurRadius: 20,)
],borderRadius:
BorderRadius.circular(15),color:
Color.fromrGBO(0,0.4)),padding: EdgeInsets.all(10),child: Slider(
max: 100,min: 0,divisions: 100,value: _value,label: _value.round().toString(),onChanged: (val) {
send_degree(u_id,item.act_id,val,m_id);
},onChangeEnd: (val) {
setState(() {
_value = val;
});
getUserStatus(); /// when i print the value here it is give me null
}),SizedBox(
height: 10,Text('$_value',style: TextStyle(
fontSize: 26,color: Colors.white))
],)
],);
}).toList()),// End activities
SizedBox(
height: 20,Text('خەلەکا ئەڤرو',textAlign: TextAlign.right,style:
TextStyle(fontSize: 26,color: Colors.yellow)),SizedBox(
height: 20,YoutubePlayer(
controller: _controller,showVideoProgressIndicator: true,progressIndicatorColor: Colors.blueAccent,],);
}
});
}
}
解决方法
您可以使用 ValueChanged onChanged 并回调要更新的页面, 您可以在参数上传递onChanged函数, 当您更改值时,使用:onChanged() 而不是您希望在页面上看到设置状态为的函数调用的更新
,我通过将两个功能合而为一来解决了这个问题:
Future<List> fetchDigrees() async {
var url =
'https://srdtraining.com/api/controller/activities/activiy_list.php?d_id=$did&m_id=$m_id&u_id=$u_id';
var response = await http.get(url);
var data = jsonDecode(response.body);
print(data);
for (var x in data) {
Digree newdigree = Digree(
x['index'],x['title_k'],x['title_a'],x['aya'],x['link'],x['activity_k'],x['activity_a'],x['act_id'],x['mydigree']);
digrees.add(newdigree);
}
return digrees;
}
void getUserStatus() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
setState(() {
username = preferences.getString('username');
});
var url =
"http://srdtraining.com/api/controller/users/status_user.php?username=$username";
var response = await http.get(url);
var data = jsonDecode(response.body);
setState(() {
newSavedDayId = int.parse(data['d_id']); ----// Set New value by getting it from API after increasing by server.
});
他们现在喜欢这个并且可以正常工作:
Future<List> fetchDigrees() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
username = preferences.getString('username');
var url1 =
"http://srdtraining.com/api/controller/users/status_user.php?username=$username";
var response1 = await http.get(url1);
var data1 = jsonDecode(response1.body);
newSavedDayId = int.parse(data1['d_id']);
print('newSavedDayId before retrun = $newSavedDayId');
var url =
'https://srdtraining.com/api/controller/activities/activiy_list.php?d_id=$did&m_id=$m_id&u_id=$u_id';
var response = await http.get(url);
var data = jsonDecode(response.body);
print(data);
for (var x in data) {
Digree newdigree = Digree(
x['index'],x['mydigree']);
digrees.add(newdigree);
}
return digrees;
}