gpt4 book ai didi

firebase - Flutter - StreamProvider - 向 Firebase 发送重复出现的 XHR 请求

转载 作者:行者123 更新时间:2023-12-04 10:03:51 27 4
gpt4 key购买 nike

我注意到自从我使用 StreamProvider 添加一个新的 Stream 以来,我的读取请求显着增加。 ,我设法通过删除流和没有重复请求来确认这一点。但我似乎无法追查为什么它重复。

注意:代码运行良好,只是这些读取请求已经过时了。

enter image description here


//get list of all open enquiries
Stream<List<EnquiryData>> get centreDashboardOpenEnquiries {
return centresCollection.document(centreID).collection('enquiries').where('enquiryStatus', whereIn: ['New', 'Contacted', 'Tour Scheduled', 'Tour Completed']).snapshots().map(_enquiryList);
}

//Map QuerySnapshot to List of EnquiryData
List<EnquiryData> _enquiryList(QuerySnapshot enquiry){
return enquiry.documents.map((doc) {
return EnquiryData(
enquiryID: doc.data['enquiryID'].toString(),
parentFirstName: doc.data['firstName'],
parentLastName: doc.data['lastName'],
parentPhoneNumber: doc.data['phoneNumber'],
parentEmail: doc.data['email'],
parentAddress: doc.data['address'],
methodOfEnquiry: doc.data['methodOfEnquiry'],
hearAboutUs: doc.data['hearAboutUs'],
specificHearAboutUs: doc.data['specificHearAboutUs'],
impressionOfCentre: doc.data['firstImpression'],
enquiryStatus: doc.data['enquiryStatus'],
created: doc.data['created'],
lastUpdated: doc.data['lastUpdated'],
);
}).toList();
}

使用 CurrentOpenDataTable 子级调用流
child: StreamProvider<List<EnquiryData>>.value(
value: EnquiryDatabaseService(centreID: widget.centreData.centreID).centreDashboardOpenEnquiries,
child: CurrentOpenDataTable(),
),

CurrentOpenDataTable()
class CurrentOpenDataTable extends StatefulWidget {
@override
_CurrentOpenDataTableState createState() => _CurrentOpenDataTableState();
}

class _CurrentOpenDataTableState extends State<CurrentOpenDataTable> {
@override
Widget build(BuildContext context) {
final enquiryData = Provider.of<List<EnquiryData>>(context) ?? [];
return DataTable(
showCheckboxColumn: false,
sortColumnIndex: 1,
sortAscending: true,
columns: [
DataColumn(
label: Text('Date'),
),
DataColumn(
label: Text('Name'),
//numeric: true,
),
DataColumn(
label: Text('Status'),
),
],
rows: _enquiryRow(context, enquiryData),
);
}
}

_dateFormat(DateTime dateTime) {
String formattedDate = DateFormat('dd/MM/yyyy').format(dateTime);
return formattedDate;
}

_enquiryRow(BuildContext context, List<EnquiryData> enquiryData) {
List<DataRow> listOfDataRows = List.generate(
enquiryData.length,
(index) => DataRow(
cells: [
DataCell(Text(_dateFormat(DateTime.fromMillisecondsSinceEpoch(
enquiryData[index].created)))),
DataCell(Text(enquiryData[index].parentFirstName +
' ' +
enquiryData[index].parentLastName)),
DataCell(Text(enquiryData[index].enquiryStatus)),
],
onSelectChanged: (bool selected) {
if (selected) {
showEnquiryDialog(context, enquiryData[index]);
}
}));
return listOfDataRows;
}

enter image description here

最佳答案

包含 MyWidget 的小部件(我们称之为 StreamProvider)正在不断重建。

您不应该创建 EnquiryDatabaseServiceStreamProvider<List<EnquiryData>>.value ,取而代之的是获取内部的流 initState .

例子

class _MyWidgetState extends State<MyWidget> {
Stream<List<EnquiryData>> _stream;

@override
initState() {
_stream = EnquiryDatabaseService(centreID: widget.centreData.centreID). centreDashboardOpenEnquiries;
}

// ... in build
StreamProvider.value(
value: _stream,
child: Container(/* ... */),
),
}

或者不要使用 .value构造函数。

StreamProvider(
create: (_) => EnquiryDatabaseService(centreID: widget.centreData.centreID). centreDashboardOpenEnquiries,
child: Container(/* ... */),
),

do's and don'ts here 上阅读更多信息
更多引用在这里 - How to deal with unwanted widget build?

关于firebase - Flutter - StreamProvider - 向 Firebase 发送重复出现的 XHR 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61690308/

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