Flutter 颤振避免多次运行FutureBuilder
在我的简单代码作为新屏幕中,不幸的是Flutter 颤振避免多次运行FutureBuilder,flutter,Flutter,在我的简单代码作为新屏幕中,不幸的是FutureBuilder工作并从方法中获取数据两次 我不确定是什么问题,我如何才能避免 class LessonDetail extends StatefulWidget { final String monthKey; final String lessonFileKey; LessonDetail({@required this.monthKey, @required this.lessonFileKey}); @override
FutureBuilder
工作并从方法中获取数据两次
我不确定是什么问题,我如何才能避免
class LessonDetail extends StatefulWidget {
final String monthKey;
final String lessonFileKey;
LessonDetail({@required this.monthKey, @required this.lessonFileKey});
@override
State<StatefulWidget> createState() {
return _LessonDetailState(monthKey, lessonFileKey);
}
}
class _LessonDetailState extends BaseState<LessonDetail> {
String monthKey;
String lessonFileKey;
_LessonDetailState(this.monthKey, this.lessonFileKey);
@override
Widget build(BuildContext context) {
return Directionality(
textDirection: TextDirection.rtl,
child: Scaffold(
body: FutureBuilder(
future: _getLessonDetail(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
PlayLessonResponse response = snapshot.data;
print(response);
}
return Center(
child: CircularProgressIndicator(),
);
}),
),
);
}
Future<PlayLessonResponse> _getLessonDetail() async {
AudioList audioList = AudioList(
'http://www.sample.com',
'aaaaa'
);
List<AudioList> lst = [audioList,audioList,audioList];
PlayLessonResponse response = PlayLessonResponse(
2,
'',
'http://www.sample.com',
'2',
lst,
1,
'ssss'
);
print('++++++++++++++++++++');
return response;
}
}
正如@Ricardo所说,您不应该直接在FutureBuilder的future方法中调用函数 相反,您应该首先在init状态下运行函数,并将响应存储在新变量中。然后才将变量分配给FutureBuilder的未来 代码示例:
class LessonDetail extends StatefulWidget {
final String monthKey;
final String lessonFileKey;
LessonDetail({@required this.monthKey, @required this.lessonFileKey});
@override
State<StatefulWidget> createState() {
return _LessonDetailState(monthKey, lessonFileKey);
}
}
class _LessonDetailState extends BaseState<LessonDetail> {
String monthKey;
String lessonFileKey;
Future<PlayLesssonResponse> _myResponse; //added this line
_LessonDetailState(this.monthKey, this.lessonFileKey);
@override
void initState() {
_myResponse = _getLessonDetail(); // added this line
super.initState();
}
@override
Widget build(BuildContext context) {
return Directionality(
textDirection: TextDirection.rtl,
child: Scaffold(
body: FutureBuilder(
future: _myResponse, //use _myResponse variable here
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
PlayLessonResponse response = snapshot.data;
print(response);
}
return Center(
child: CircularProgressIndicator(),
);
}),
),
);
}
Future<PlayLessonResponse> _getLessonDetail() async {
AudioList audioList = AudioList(
'http://www.sample.com',
'aaaaa'
);
List<AudioList> lst = [audioList,audioList,audioList];
PlayLessonResponse response = PlayLessonResponse(
2,
'',
'http://www.sample.com',
'2',
lst,
1,
'ssss'
);
print('++++++++++++++++++++');
return response;
}
}
class LessonDetail扩展了StatefulWidget{
最终字符串蒙特基;
最后一个字符串lessonFileKey;
LessonDetail({@required this.monthKey,@required this.lessonFileKey});
@凌驾
状态createState(){
返回LessonDetailState(monthKey,lessonFileKey);
}
}
类_LessonDetailState扩展了BaseState{
斯特林·蒙特基;
字符串lessonFileKey;
Future _myResponse;//添加了此行
_LessonDetailState(this.monthKey,this.lessonFileKey);
@凌驾
void initState(){
_myResponse=\u getLessonDetail();//添加了此行
super.initState();
}
@凌驾
小部件构建(构建上下文){
返回方向性(
textDirection:textDirection.rtl,
孩子:脚手架(
正文:未来建设者(
future:\u myResponse,//在此处使用\u myResponse变量
生成器:(上下文,快照){
if(snapshot.connectionState==connectionState.done){
PlayLessonResponse=snapshot.data;
打印(回复);
}
返回中心(
子对象:CircularProgressIndicator(),
);
}),
),
);
}
Future\u getLessonDetail()异步{
AudioList=AudioList(
'http://www.sample.com',
“aaaaa”
);
List lst=[audioList,audioList,audioList];
PlayLessonResponse=PlayLessonResponse(
2.
'',
'http://www.sample.com',
'2',
lst,
1.
“ssss”
);
印刷("印刷"),;
返回响应;
}
}
正如@Ricardo所说,您不应该直接在FutureBuilder的future方法中调用函数
相反,您应该首先在init状态下运行函数,并将响应存储在新变量中。然后才将变量分配给FutureBuilder的未来
代码示例:
class LessonDetail extends StatefulWidget {
final String monthKey;
final String lessonFileKey;
LessonDetail({@required this.monthKey, @required this.lessonFileKey});
@override
State<StatefulWidget> createState() {
return _LessonDetailState(monthKey, lessonFileKey);
}
}
class _LessonDetailState extends BaseState<LessonDetail> {
String monthKey;
String lessonFileKey;
Future<PlayLesssonResponse> _myResponse; //added this line
_LessonDetailState(this.monthKey, this.lessonFileKey);
@override
void initState() {
_myResponse = _getLessonDetail(); // added this line
super.initState();
}
@override
Widget build(BuildContext context) {
return Directionality(
textDirection: TextDirection.rtl,
child: Scaffold(
body: FutureBuilder(
future: _myResponse, //use _myResponse variable here
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
PlayLessonResponse response = snapshot.data;
print(response);
}
return Center(
child: CircularProgressIndicator(),
);
}),
),
);
}
Future<PlayLessonResponse> _getLessonDetail() async {
AudioList audioList = AudioList(
'http://www.sample.com',
'aaaaa'
);
List<AudioList> lst = [audioList,audioList,audioList];
PlayLessonResponse response = PlayLessonResponse(
2,
'',
'http://www.sample.com',
'2',
lst,
1,
'ssss'
);
print('++++++++++++++++++++');
return response;
}
}
class LessonDetail扩展了StatefulWidget{
最终字符串蒙特基;
最后一个字符串lessonFileKey;
LessonDetail({@required this.monthKey,@required this.lessonFileKey});
@凌驾
状态createState(){
返回LessonDetailState(monthKey,lessonFileKey);
}
}
类_LessonDetailState扩展了BaseState{
斯特林·蒙特基;
字符串lessonFileKey;
Future _myResponse;//添加了此行
_LessonDetailState(this.monthKey,this.lessonFileKey);
@凌驾
void initState(){
_myResponse=\u getLessonDetail();//添加了此行
super.initState();
}
@凌驾
小部件构建(构建上下文){
返回方向性(
textDirection:textDirection.rtl,
孩子:脚手架(
正文:未来建设者(
future:\u myResponse,//在此处使用\u myResponse变量
生成器:(上下文,快照){
if(snapshot.connectionState==connectionState.done){
PlayLessonResponse=snapshot.data;
打印(回复);
}
返回中心(
子对象:CircularProgressIndicator(),
);
}),
),
);
}
Future\u getLessonDetail()异步{
AudioList=AudioList(
'http://www.sample.com',
“aaaaa”
);
List lst=[audioList,audioList,audioList];
PlayLessonResponse=PlayLessonResponse(
2.
'',
'http://www.sample.com',
'2',
lst,
1.
“ssss”
);
印刷("印刷"),;
返回响应;
}
}
你能尝试使用builder:(BuildContext context,asyncsapshot snapshot){
@TinusJackson没有任何变化那么我不确定,期待着答案,考虑BaseState
中发生了什么?@TinusJackson我用BaseState
类内容更新了代码你能尝试使用builder:(BuildContext上下文,异步快照){
@TinusJackson没有任何变化那么我不确定,期待着答案,尽管BaseState
?@TinusJackson我用BaseState
类内容更新了代码这东西是金子!遗憾的是,即使是flifter官方文档也没有培养出这种方法……太棒了,谢谢!这东西是金子!遗憾的是flutt呃,官方文件并没有培养这种方法……太棒了,谢谢!
class LessonDetail extends StatefulWidget {
final String monthKey;
final String lessonFileKey;
LessonDetail({@required this.monthKey, @required this.lessonFileKey});
@override
State<StatefulWidget> createState() {
return _LessonDetailState(monthKey, lessonFileKey);
}
}
class _LessonDetailState extends BaseState<LessonDetail> {
String monthKey;
String lessonFileKey;
Future<PlayLesssonResponse> _myResponse; //added this line
_LessonDetailState(this.monthKey, this.lessonFileKey);
@override
void initState() {
_myResponse = _getLessonDetail(); // added this line
super.initState();
}
@override
Widget build(BuildContext context) {
return Directionality(
textDirection: TextDirection.rtl,
child: Scaffold(
body: FutureBuilder(
future: _myResponse, //use _myResponse variable here
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
PlayLessonResponse response = snapshot.data;
print(response);
}
return Center(
child: CircularProgressIndicator(),
);
}),
),
);
}
Future<PlayLessonResponse> _getLessonDetail() async {
AudioList audioList = AudioList(
'http://www.sample.com',
'aaaaa'
);
List<AudioList> lst = [audioList,audioList,audioList];
PlayLessonResponse response = PlayLessonResponse(
2,
'',
'http://www.sample.com',
'2',
lst,
1,
'ssss'
);
print('++++++++++++++++++++');
return response;
}
}