我有一个类,我想从Firestore数据库中显示一个文档列表。我已经创建了一个方法,_getTrxnData,以获取文档,但它从来没有得到执行。
如何在页面加载时执行此方法?
我不知道这种事的执行程序是什么。
class CompanyDashboardScreen extends ConsumerStatefulWidget {
static const String id = 'user_dashboard_screen';
const CompanyDashboardScreen({super.key});
@override
ConsumerState<CompanyDashboardScreen> createState() =>
_CompanyDashboardScreenState();
}
class _CompanyDashboardScreenState
extends ConsumerState<CompanyDashboardScreen> {
bool showSpinner = false;
late List<Map<String, dynamic>> itemsTrxn;
bool isLoaded = false;
_getTrxnData() async {
var collectionTrxn = FirebaseFirestore.instance
.collection('company')
.doc(ref.read(globalsNotifierProvider).companyId)
.collection('trxns');
List<Map<String, dynamic>> tempList = [];
// ignore: unused_local_variable
var dataTrxn = await collectionTrxn.get();
dataTrxn.docs.forEach((element) {
tempList.add(element.data());
});
setState(() {
itemsTrxn = tempList;
isLoaded = true;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: CustomAppBar(),
drawer: Drawer(
child: ListView(
children: <Widget>[
ListTile(
title: const Text("User Profile"),
onTap: () {
MainScreen.of(context)
?.setIndex(3); // Added this for BottomNavigationBar sync
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => const UserProfileScreen()));
},
),
],
),
),
body: SafeArea(
child: isLoaded
? ListView.builder(
itemCount: itemsTrxn.length,
itemBuilder: (context, index) {
_getTrxnData(); <<<< METHOD HERE
return Padding(
padding: const EdgeInsets.all(8.0),
child: ListTile(
isThreeLine: true,
title: Row(
children: [
Text(
'Client: ${itemsTrxn[index]['clientFName'] ?? 'n/a'} ${itemsTrxn[index]['clientLName'] ?? 'n/a'}',
style: const TextStyle(
fontWeight: FontWeight.w900,
color: Colors.blueAccent),
),
],
),
subtitle: Text.rich(
TextSpan(
text:
'${itemsTrxn[index]['propertyAddress'] ?? 'n/a'}, '
'${itemsTrxn[index]['propertyCity'] ?? 'n/a'}, '
'${itemsTrxn[index]['propertyState'] ?? 'n/a'}',
children: <TextSpan>[
TextSpan(
text:
'\nPrice: ${itemsTrxn[index]['contractPrice'] == null ? 'n/a' : NumberFormat.simpleCurrency().format(itemsTrxn[index]['contractPrice'])}\nStatus: ${itemsTrxn[index]['trxnStatus'] ?? 'n/a'}',
style: const TextStyle(
fontWeight: FontWeight.w900,
color: Colors.blueGrey),
)
],
),
),
trailing: Text(
'MLS#: ${itemsTrxn[index]['mlsNumber'] ?? 'n/a'}\n${itemsTrxn[index]['clientType']}'),
onTap: () {
//MainScreen.of(context)?.setIndex(2); // Added this for BottomNavigationBar sync
ref
.read(globalsNotifierProvider.notifier)
.updatenewTrxn(false);
ref
.read(globalsNotifierProvider.notifier)
.updatecurrentTrxnId(
itemsTrxn[index]['trxnId']);
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => TransactionDetailScreen(
itemsTrxn[index]['trxnId'])));
},
),
);
},
)
: const Text('No Date')),
),
);
}
}
字符串
1条答案
按热度按时间xzv2uavs1#
您可以使用FutureBuilder小部件来执行该方法。