簡體   English   中英

沒有為“FirebaseApp”類型定義“配置”方法

[英]The method 'configure' isn't defined for the type 'FirebaseApp'

我正在使用 firebase 身份驗證進行用戶登錄,但收到 FirebaseApp 配置方法的此錯誤消息。
下面是我的代碼。

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import '../Questions.dart';

class Tasks extends StatefulWidget {
  Tasks({this.uid: ""});
  final String uid;
  @override
  TasksState createState() => TasksState(uid);
}

class TasksState extends State<Tasks> {
  var now = DateTime.now();
  bool timeUp = true;
  TasksState(this.userId);

  Map<String, dynamic> identifiers = Map.from({
    "answered": false,
    "value": false,
    "notDone1": true,
    "notDone2": true,
    "defNumber": -1,
    "hour": null
  });
  FirebaseAuth auth = FirebaseAuth.instance;

  String userId;
  DocumentReference ansRef;

  Future<void> configure() async {
    final FirebaseApp app = await FirebaseApp.configure(
        name: 'companionbeta',
        options: const FirebaseOptions(
            googleAppID: '*******************************',
            apiKey: '********************************',
            projectID: '*******************'
));
    final FirebaseFirestore firestore = Firestore.instance();
    await firestore.settings(timestampsInSnapshotsEnabled: true);
  }

  bool isDepressed(DocumentSnapshot document) {
    document.data.putIfAbsent("q1", document["defNumber"]);
    if (document["q1"] < 3) return true;
    return false;
  }

  @override
  void initState() {
    configure();
    ansRef = Firestore.instance.collection("answers").document(userId);
    addData();
    super.initState();
  }

  void updateTime(DocumentSnapshot document) async {
    if (document["hour"] == null)
      await document.reference.updateData({"hour": now.hour});
    assert(now.hour != null);
    if (document["hour"] < 24) {
      setState(() {
        timeUp = true;
      });
      await document.reference.updateData({"hour": document["hour"] + 24});
    }
  }

  void addData() async {
    ansRef.get().then((DocumentSnapshot ds) {
      ds.data.addAll(identifiers);
      updateTime(ds);
      isDepressed(ds);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Material(
        child: StreamBuilder(
            stream: Firestore.instance.collection("answers").snapshots(),
            builder: (context, snapshot) {
              if (!snapshot.hasData) return Text("Loading..");
              DocumentSnapshot doc = snapshot.data.documents[0];
              Map<String, bool> check = Map.from({
                "answered": (doc["answered"] == null) ? false : true,
                "notDone1": (doc["notDone1"] == null) ? false : true,
                "notDone2": (doc["notDone2"] == null) ? false : true,
                "q1": (doc["q1"] == null) ? false : true,
              });
              return builder(context, doc, check);
            }));
  }

  Widget builder(BuildContext context, DocumentSnapshot document,
      Map<String, bool> check) {
    return Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
      (check["answered"] && timeUp && !document["answered"])
          ? askQuestion(document)
          : Container(),
      Flexible(
          child: Card(
        child: ListView(
          children: <Widget>[
            (check["notDone1"] &&
                    check["q1"] &&
                    isDepressed(document) &&
                    document["notDone1"])
                ? ListTile(
                    contentPadding: EdgeInsets.all(10.0),
                    leading: Icon(Icons.people),
                    title: Text("Meet more people"),
                    subtitle: Text("uno dos tres"),
                    trailing: Checkbox(
                      value: false,
                      onChanged: (value) {
                        setState(() {
                          if (value)
                            document.reference.updateData({"notDone1": false});
                        });
                      },
                    ),
                  )
                : Container(),
            (check["notDone2"] &&
                    check["q1"] &&
                    isDepressed(document) &&
                    document["notDone2"])
                ? ListTile(
                    contentPadding: EdgeInsets.symmetric(horizontal: 10.0),
                    leading: Icon(Icons.library_music),
                    title: Text("Listen to tunes"),
                    subtitle: Text("uno dos tres"),
                    trailing: Checkbox(
                      value: document["value"],
                      onChanged: (value) {
                        setState(() {
                          if (value)
                            document.reference.updateData({"notDone2": false});
                        });
                      },
                    ),
                  )
                : Container(),
            (check["q1"] &&
                    check["notDone1"] &&
                    isDepressed(document) &&
                    !document["notDone1"])
                ? Card(
                    child: Expanded(
                    child: Text("Yay! We are happy today"),
                  ))
                : Container()
          ],
        ),
        elevation: 2.0,
        margin: EdgeInsets.symmetric(horizontal: 12.0, vertical: 6.0),
      ))
    ]);
  }

  Widget askQuestion(DocumentSnapshot document) {
    return Card(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          const ListTile(
            leading: Icon(Icons.question_answer),
            title: Text('Ready to answer a few questions?'),
            subtitle: Text('It will only take a minute'),
          ),
          ButtonTheme.bar(
            // make buttons use the appropriate styles for cards
            child: ButtonBar(
              children: <Widget>[
                FlatButton(
                  child: const Text('Yes!'),
                  onPressed: () {
                    Navigator.of(context).push(MaterialPageRoute(
                        builder: (context) => Questions(document)));
                  },
                ),
                FlatButton(
                  child: const Text('Not now'),
                  onPressed: () {
                    setState(() {
                      document.reference.updateData({"answered": true});
                    });
                  },
                ),
              ],
            ),
          ),
        ],
      ),
      elevation: 2.0,
      margin: EdgeInsets.all(12.0),
    );
  }
}

對於隱私問題,我刪除了 googleID、appkey 和 projectId,所以忽略它。

錯誤信息。

沒有為“FirebaseApp”類型定義“配置”方法。 嘗試將名稱更正為現有方法的名稱,或定義名為“configure”的方法。

FirebaseApp沒有名為configure的方法,您需要使用initializeApp

    final FirebaseApp app = await FirebaseApp.initializeApp(
        name: 'companionbeta',
        options: const FirebaseOptions(
            googleAppID: '*******************************',
            apiKey: '********************************',
            projectID: '*******************'
));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM