简体   繁体   English

如何从 Flutter firebase firestore 数据库中获取这些数据?

[英]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的指导,其中:

  1. Generates a firebase-options.dart file by using the flutterfire CLI,使用flutterfire CLI 生成一个firebase-options.dart文件,

  2. Then import this firebase-options.dart file into your main.dart .然后将此 firebase firebase-options.dart文件导入您的main.dart

  3. 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.

相关问题 如何从 Firebase (Firestore) 获取数据到 Flutter 中的模型列表中? - How to get data from Firebase (Firestore) into list of models in Flutter? 如何从 Flutter 中的 Firebase Firestore 正确获取数据? - How to properly get data from Firebase Firestore in Flutter? flutter firestore无法从数据库获取数据 - flutter firestore unable to get data from database 使用 Flutter Firebase Firestore 从数据库获取地理点数据 - Getting Geopoint Data from Database Using Flutter Firebase Firestore 如何使用 excel 或 google 电子表格或 CSV 使用 excel 导入 firebase firestore 数据库中的批量数据 - How to import bulk data in firebase firestore database from excel or google spreadsheet or CSV using flutter 如何从 Firebase 实时数据库中获取数据到 Flutter 的列表中? - How to get data from Firebase Realtime Database into list in Flutter? 如何将切换开关按钮从颤动添加到 firebase firestore 数据库 - How to add the toggle switch button from flutter to firebase firestore database Flutter/Firebase/Firestore - 注册不会将数据推送到数据库 - Flutter/Firebase/Firestore - Signup not pushing data to database 如何使用graphql从Firebase使用flutter从Cloud Firestore获取数据? - How to use graphql to get data from Cloud Firestore from Firebase using flutter? 如何使用 FlutterFire Firestore 和 Flutter 2.0 和 Null 从 Firebase 获取数据 - How do I get data from Firebase using FlutterFire Firestore and Flutter 2.0 with Null Safety
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM