[英]How to get this data from Flutter firebase firestore database?
i am new to coding, using flutter firebase firestore i want to fetch data (currently manually added from server. Attaching my code我是编码新手,使用flutter firebase firestore我想获取数据(目前是从服务器手动添加的。附加我的代码
Main.dart主要.dart
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:quizzy/screens/PhysicsNew.dart';
import 'package:quizzy/screens/showAnswer.dart';
import 'package:quizzy/screens/home.dart';
import 'package:quizzy/screens/physics.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(const Quizzy());
}
class Quizzy extends StatelessWidget {
const Quizzy({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(title: 'Flutter Demo', initialRoute: '/', routes: {
'/': (context) => Home(),
'/physics': (context) => Physics(),
'/physicsNew': (context) => PhysicsNew(),
'/showAnswers': (context) => ShowAnswer(),
});
}
}
Physics.dart物理.dart
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
class PhysicsNew extends StatefulWidget {
const PhysicsNew({Key? key}) : super(key: key);
@override
State<PhysicsNew> createState() => _PhysicsNewState();
}
class _PhysicsNewState extends State<PhysicsNew> {
final Stream<QuerySnapshot> questionList =
FirebaseFirestore.instance.collection('QuestionList').snapshots();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Title')),
body: Container(
child: StreamBuilder<QuerySnapshot>(
stream: questionList,
builder: (
BuildContext context,
AsyncSnapshot<QuerySnapshot> Snapshot,
) {
//
if (Snapshot.hasError) {
return Text('Something went wrong');
}
if (Snapshot.connectionState == ConnectionState.waiting) {
Text('Loading');
}
final data = Snapshot.requireData;
return ListView.builder(
itemCount: data.size,
itemBuilder: (context, index) {
return Text(data.docs[index]['Question']);
});
})),
);
}
}
Pubspec.yaml发布规范.yaml
name: quizzy
description: Made by *****.com
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
#
version: 1.0.0+1
environment:
sdk: ">=2.17.3 <3.0.0"
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
firebase_core: ^1.3.0
cloud_firestore: ^2.2.2
firebase_auth: ^3.3.20
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^2.0.0
flutter:
uses-material-design: true
I am trying fetch data manually added from firestore .我正在尝试从 firestore 手动添加数据。 attaching my database details in image.在图像中附加我的数据库详细信息。
I am new to flutter, tried various things but it gets complicated and messy, so someone who could help me, Help is Apreciated.我是新来的,尝试了各种事情,但它变得复杂和混乱,所以有人可以帮助我,感谢帮助。 ! !
Error I am getting in debug Console我在调试控制台中遇到的错误
E/flutter ( 6922): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: [core/not-initialized] Firebase has not been correctly initialized.
E/flutter ( 6922): Usually this means you've attempted to use a Firebase service before calling `Firebase.initializeApp`.
E/flutter ( 6922): View the documentation for more information: https://firebase.flutter.dev/docs/overview#initialization
E/flutter ( 6922): #0 MethodChannelFirebase.initializeApp
E/flutter ( 6922): <asynchronous suspension>
E/flutter ( 6922): #1 Firebase.initializeApp
E/flutter ( 6922): #
2 main 2 主要
Usually this means you've attempted to use a Firebase service before calling
Firebase.initializeApp
.通常这意味着您在调用Firebase.initializeApp
之前尝试使用 Firebase 服务。
You're not passing any configuration data to the call to initializeApp
, which means the Firebase SDK can't initialize its connection to your project on the server.您没有将任何配置数据传递给对initializeApp
的调用,这意味着 Firebase SDK 无法初始化其与服务器上您的项目的连接。
I recommend following the guidance in the Firebase documentation on setting up the SDK , which:我建议遵循 Firebase 文档中关于设置 SDK的指导,其中:
Generates a firebase-options.dart
file by using the flutterfire
CLI,使用flutterfire
CLI 生成一个firebase-options.dart
文件,
Then import this firebase-options.dart
file into your main.dart
.然后将此 firebase firebase-options.dart
文件导入您的main.dart
。
And finally passes the configuration options to the Firebase SDK like this:最后将配置选项传递给 Firebase SDK,如下所示:
await Firebase.initializeApp( options: DefaultFirebaseOptions.currentPlatform, );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.