I have a flutter project that I want to migrate to the newest dependencies (Firestore, FirebaseAuth, FirebaseStorage, etc.). I migrated all the code and also updated my library to be able to launch the app. In a lot of my firebase collections I do not have defined fields and previously when I request from Firebase the field it just returns null. Now instead, it creates an error - Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist and the application cannot function properly. I would appreciate solutions that would allow me to update my dependencies but not change the code base as much.
Dependencies in Pubspec.yaml file: cupertino_icons: ^1.0.4 cloud_firestore: ^3.1.0 firebase_auth: ^3.2.0 curved_navigation_bar: ^1.0.1 grouped_list: ^4.1.0 random_color: ^1.0.6-nullsafety intl: ^0.17.0 firebase_storage: ^10.1.0 image_picker: ^0.8.4 4 flutter_staggered_grid_view: ^0.4.1 carousel_slider: ^4.0.0 csv: ^5.0.0 path_provider: ^2.0.7 excel: ^2.0.0-null-safety-3 flutter_email_sender: ^5.0.2 firebase_core: ^1.10.0 firebase_messaging: ^11.1.0 auto_size_text: ^3.0.0 flutter_icons: ^1.1.0 camera: ^0.9.4 4 qr_code_scanner: ^0.6.1 package_info: ^2.0.2 url_launcher: ^6.0.13
Example Streambuilder code: StreamBuilder( stream: stream, builder: (ctx, queryDetailSnapShot) { if (queryDetailSnapShot.connectionState == ConnectionState.waiting) { return Center(child: CircularProgressIndicator()); }
var priceDetail = queryDetailSnapShot.data.docs;
return ListView.builder(
reverse: false,
itemCount: priceDetail.length,
itemBuilder: (ctx, index) {
if (_selectedBottomIndex == 1) {
return CostExpenseRequestContainer(
widget.storenameUser,
widget.storeCostAcess,
widget.userName,
widget.userEmail,
widget.userId,
widget.userLanguage,
priceDetail[index]['Storename'],
priceDetail[index]['Created_dt'],
priceDetail[index]['Expense_type_en'],
priceDetail[index]['Expense_type_th'],
priceDetail[index]['Expense_amount_org'],
priceDetail[index]['Expense_amount_approved'],
priceDetail[index]['Visible_to'],
priceDetail[index]['Expense_status'],
priceDetail[index]['Photo_url'],
priceDetail[index]['Service_dt'],
priceDetail[index]['Partner_account_number'],
priceDetail[index]['Partner_value_th'],
priceDetail[index]['Partner_value_en'],
priceDetail[index].id,
key: ValueKey(priceDetail[index].id,),
);
} else
return CostExpenseTrContainer(
widget.storenameUser,
widget.storeCostAcess,
widget.userName,
widget.userEmail,
widget.userId,
widget.userLanguage,
priceDetail[index]['Storename'],
priceDetail[index]['Created_dt'],
priceDetail[index]['Service_dt'],
priceDetail[index]['Expense_type_en'],
priceDetail[index]['Expense_type_th'],
priceDetail[index]['Expense_amount'],
priceDetail[index]['Reversal_flg'],
priceDetail[index]['Visible_to'],
priceDetail[index].id,
priceDetail[index]['Parent_record_id'],
priceDetail[index]['Partner_account_number'],
key: ValueKey(
priceDetail[index].id,
),
);
});
}),
Error Message: Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist
Thanks,
CodePudding user response:
Earlier you had to call the field in document snapshot like this
docSnapshot['field']
But in the recent version, you have to call the data function to get the data as a map then access the required field. Example
docSnapshot.data()['field']
CodePudding user response:
Since the field that was creating an issue for me was 'Partner_account_number', I needed to check the if the field exists before calling. This was the quickest fix I could find that need to apply throughout the application.
priceDetail[index].data().containsValue('Partner_account_number') ==true? priceDetail[index]['Partner_account_number'] :null,