FirebaseFirestore无法被Flutter (Android Studio)识别



以下是我的代码,略微改编自flutter文档中的示例:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
@override
Widget build(BuildContext context) {
CollectionReference items = FirebaseFirestore.instance.collection('item');
return FutureBuilder<DocumentSnapshot>(
future: items.doc(itemId).get(),
builder:
(BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.hasError) {
return Text("Something went wrong");
}
if (snapshot.connectionState == ConnectionState.done) {
Map<String, dynamic> data = snapshot.data.data();
return data['out_of_stock_location_ids'];// ${data['last_name']}");
}
return Text("loading");
},
);

}

代码"FirebaseFirestore"给出一个错误,因为它不被识别为我导入的库的一部分。导入'package:firebase_core/firebase_core.dart';是灰色的,可能是不合适的。我在网上找不到这种情况发生的原因,也找不到该怎么办。

如果您使用的是cloud_firestore: 0.14.0及以上版本,则需要使用FirebaseFirestore&doc(),如果cloud_firestore: 0.13.7+1及以下,你需要使用Firestore&document().

你提到的文档是更新后的我相信你现在使用的是cloud_firestore的较低版本。如果你想使用cloud_firestore的更高或最新版本,你必须在pubspec.yaml中更改firestore的版本,然后访问pub.dev中的firestore的更改日志,查看firestore的每一次更改和更新。

我不确定但你可能没有在main方法中初始化firebase你必须使用firebase core来初始化

import core tomain.dart

Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}

看来文档已经过时了。我不得不将FirebaseFirestore更改为Firestore,并将文档更改为文档。我不知道人们怎么知道该怎么做。

相关内容

最新更新