要在其
dispose()
方法中安全地引用窗口小部件的祖先,请通过在窗口小部件的dependOnInheritedWidgetOfExactType()
方法中调用didChangeDependencies()
来保存对祖先的引用。
我如何保存参考,我不知道,我是新手。
这是我的示例代码:图像井井有条 First
class QuestionFeed extends StatefulWidget {
final FirebaseUser loggedInUser;
QuestionFeed({ this.loggedInUser });
@override
_QuestionFeedState createState() => _QuestionFeedState();
}
class _QuestionFeedState extends State<QuestionFeed> {
List<Widget> questions;
List<String> followingsList=[];
bool x;
final _scaffoldKey=GlobalKey<ScaffoldState>();
retrieveTimeLine()async{
QuerySnapshot querySnapshot =await
questionsFeedReference.orderBy("timeStamp",descending:
true).getDocuments();
setState(() {
List<Widget> allQuestions=querySnapshot.documents.where((element) {
if(element["type"]=="question" ){
x=true;
return true;
}
else if(element["type"]=="answer"){
x=false;
return true;
}
else{
return false;
}
}).map((document)=>x?Question.fromDocument(document):
StreamBuilder<DocumentSnapshot>(
stream: answersReference.document("038281b3-3842-4ea9-b4d8-
3155a571826e").snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(
backgroundColor: Colors.lightBlueAccent,
),
);
}
NotusDocument _document;
ZefyrController _zefyrController;
FocusNode _focusNode;
final messages = snapshot.data;
final messageText = messages.data['answer'];
_document =NotusDocument.fromJson( jsonDecode(messageText));
_zefyrController = ZefyrController(_document);
_focusNode=FocusNode();
return Container(
height: 400,
child: ZefyrScaffold(
child: ZefyrEditor(
padding: EdgeInsets.all(16),
controller: _zefyrController,
mode: ZefyrMode.view,
focusNode: _focusNode,
imageDelegate: MyAppZefyrImageDelegate(),
physics: NeverScrollableScrollPhysics(),
),
),
);
},
)
).toList();
this.questions=allQuestions;
});
}
@override
void initState() {
// TODO: implement initState
super.initState();
retrieveTimeLine();
}
createUserTimeLine(){
retrieveTimeLine();
if(questions==null){
return circularProgress();
}
else{
return ListView(children: questions,);
}
}
@override
Widget build(context) {
return Scaffold(
key: _scaffoldKey,
appBar: header(
context,
isAppTitle: true,
),
body: RefreshIndicator(
child: createUserTimeLine(),onRefresh()=>retrieveTimeLine(),),
);
}
}
我正在使用Zefyr Editor,然后检索其数据并将其显示在Container中