请帮助,无法获取数据(产品)
I/扑动(7142):获取产品时出错:错误状态:字段不存在于DocumentSnapshotPlatform中I/flutter(7142):异常错误:状态错误:字段不存在于文档快照平台E/flutter中(7142):[错误:flutter/运行时/dart_vm_initializer.cc(41)]未处理的异常:异常错误:异常错误:状态错误:DocumentSnapshotPlatform中不存在字段
import 'dart:io';
import 'package:intl/intl.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter_e_commerce_app/Config/size_config.dart';
import 'package:flutter_e_commerce_app/Constants/colors.dart';
import 'package:flutter_e_commerce_app/Controller/app_controller.dart';
import 'package:flutter_e_commerce_app/Providers/categories.dart';
import 'package:flutter_e_commerce_app/Providers/favorites.dart';
import 'package:flutter_e_commerce_app/Providers/products.dart';
import 'package:flutter_e_commerce_app/Providers/user.dart';
import 'package:flutter_e_commerce_app/Screens/Auth/auth_screen.dart';
import 'package:flutter_e_commerce_app/Screens/load_data.dart';
import 'package:flutter_e_commerce_app/Widgets/dialogs_and_snackbars.dart';
import 'package:provider/provider.dart';
import 'Home/home_screen.dart';
Timestamp timestamp = Timestamp.now();
String simplyFormat({required DateTime time, bool dateOnly = false}) {
String year = time.year.toString();
// Add "0" on the left if month is from 1 to 9
String month = time.month.toString().padLeft(2, '0');
// Add "0" on the left if day is from 1 to 9
String day = time.day.toString().padLeft(2, '0');
// Add "0" on the left if hour is from 1 to 9
String hour = time.hour.toString().padLeft(2, '0');
// Add "0" on the left if minute is from 1 to 9
String minute = time.minute.toString().padLeft(2, '0');
// Add "0" on the left if second is from 1 to 9
String second = time.second.toString();
// If you only want year, month, and date
if (dateOnly == false) {
return "$year-$month-$day $hour:$minute:$second";
}
// return the "yyyy-MM-dd HH:mm:ss" format
return "$year-$month-$day";
}
final date = simplyFormat(time: timestamp.toDate(), dateOnly: true);
var key=UniqueKey().toString();
var id=key;
int counter=0;
class MainScreen extends StatefulWidget {
static const routeName = '/main-screen';
const MainScreen({Key? key}) : super(key: key);
@override
State<MainScreen> createState() => _MainScreenState();
}
class _MainScreenState extends State<MainScreen> {
bool _isLoading = false;
bool _isInit = true;
bool hasConnection = true;
@override
void didChangeDependencies() {
if (_isInit) {
fetchData();
}
_isInit = false;
super.didChangeDependencies();
}
fetchData() async {
setState(() {
_isLoading = true;
hasConnection = true;
});
try {
// final result =
await InternetAddress.lookup('google.com');
await Provider.of<UserData>(context, listen: false).fetchUserData();
await Provider.of<Categories>(context, listen: false).fetchCategories();
await Provider.of<Products>(context, listen: false).fetchProducts();
await Provider.of<Favorites>(context, listen: false).fetchFavorites();
print(date);
await FirebaseFirestore.instance.collection('users').doc(FirebaseAuth.instance.currentUser!.uid).collection('TotalLikes').where('Date',isEqualTo: date).get().then((value) {
if(value.docs.isEmpty){
FirebaseFirestore.instance.collection('users').doc(FirebaseAuth.instance.currentUser!.uid).collection('TotalLikes').doc(key).set({
"Count":counter,
"Date": date
});
}else if(value.docs.isNotEmpty){
print('exists');
for(var doc in value.docs){
id = doc.id;
counter = doc['Count'];
}
}
});
setState(() {
_isLoading = false;
});
} on SocketException catch (_) {
// print('not connected');
setState(() {
hasConnection = false;
});
customDialog(
context: context,
text: 'Something goes wrong.\nCheck your connection and try again!',
);
} catch (e) {
print(e.toString());
throw Exception(e);
}
}
@override
Widget build(BuildContext context) {
SizeConfig().init(context);
// return HomeScreen();
if (_isLoading) {
return Material(
child: Center(
child: hasConnection
? Text(
'Loading Products...',
style: Theme.of(context).textTheme.bodyText1,
)
: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Something went wrong..\nCheck your connection and try again!',
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.bodyText1,
),
ElevatedButton(
onPressed: () {
fetchData();
},
style: ElevatedButton.styleFrom(
primary: AppColors.davysGrey,
),
child: Text('Try Again'),
),
],
),
),
);
} else {
return HomeScreen();
}
}
}
1条答案
按热度按时间biswetbf1#
当我从firebase获取数据时,我也遇到了同样的问题,当你从firebase获取数据时,这个错误会显示出来,但firebase中没有可用的字段。
示例:-我试图从firebase中获取名称字段,但没有可用于名称或字段数据。
希望这些信息对你有所帮助。