当前位置: 首页 > 知识库问答 >
问题:

尚未创建Firebase应用程序-调用Firebase.InitializeApp[duplicate]

司马晋
2023-03-14

我有点明白这个错误,但不管我怎么说,它都不起作用...我试图从FireBase中只返回一个字符串和一个数字值。我也做了所有的要求。android文件夹中的build.graddle和android/App文件夹中的build.gradle。一切。除此之外,我还做了:cloud_firestore:

对pubspec.yaml的依赖关系。现在我的main.dart看起来是这样的,但是它给出了太多的错误,我无法粘贴到这里。很长。我用这个视频作为基础,但它不起作用…

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}
class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  Widget _buildListItem(BuildContext context, DocumentSnapshot document) {
    return ListTile(
      title: Row(
        children: [
          Expanded(
            child: Text(
              document.data()['adSoyad'],
            ),
          ),
          Container(
            decoration: const BoxDecoration(
              color: Color(0xffddddff),
            ),
            padding: const EdgeInsets.all(10.0),
            child: Text(
              document.data()['yas'].toString(),
              style: Theme.of(context).textTheme.headline4,
            ),
          ),
        ],
      ),
      onTap: () {
        print("bisey");
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: StreamBuilder(
          stream: FirebaseFirestore.instance.collection('tablolar').snapshots(),
          builder: (context, snapshot) {
            if (!snapshot.hasData) return const Text('Loading...');
            return ListView.builder(
              itemExtent: 80.0,
              itemCount: snapshot.data.documents.length,
              itemBuilder: (context, index) =>
                  _buildListItem(context, snapshot.data.documents[index]),
            );
          }),
    );
  }
}

共有1个答案

佘缪文
2023-03-14

在主函数中,添加这两行并使函数异步。像这样

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

还有一点,您还需要将firebase_core添加到项目中。否则firebase将抛出错误

在pubsec.yaml文件中添加firebase核心

  firebase_core: ^0.5.3
import 'package:firebase_core/firebase_core.dart';
 类似资料: