dart 运行带有firebase依赖项的flutter应用时出错

iqxoj9l9  于 2023-07-31  发布在  Flutter
关注(0)|答案(1)|浏览(135)

由于我在应用程序中添加了从firestore获取数据的功能,因此在运行flutter run时,我会收到以下错误:

Launching lib/main.dart on 2109119DG in debug mode...
../../../../.pub-cache/hosted/pub.dev/cloud_firestore_platform_interface-5.7.7/lib/src/platform_interface/utils/load_bundle_task_state.dart:13:13: Error: Method not found: 'FallThroughError'.
      throw FallThroughError();
            ^^^^^^^^^^^^^^^^
../../../../.pub-cache/hosted/pub.dev/firebase_core-1.24.0/lib/src/firebase_app.dart:18:25: Error: Member not found: 'FirebaseAppPlatform.verifyExtends'.
    FirebaseAppPlatform.verifyExtends(_delegate);
                        ^^^^^^^^^^^^^
Target kernel_snapshot failed: Exception

FAILURE: Build failed with an exception.

* Where:
Script '/home/user/.flutter/flutter/packages/flutter_tools/gradle/flutter.gradle' line: 1201

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command '/home/user/.flutter/flutter/bin/flutter'' finished with non-zero exit value 1

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 3s
Running Gradle task 'assembleDebug'...                              3.9s
Exception: Gradle task assembleDebug failed with exit code 1

字符串
下面是主要的.dart:

import 'package:flutter/material.dart';
import 'package:precio_luz/screens/main_screen.dart';

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

class PrecioLuz extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'PrecioLuz',
      theme: ThemeData(
        primarySwatch: Colors.grey,
      ),
      home: MainScreen(),
    );
  }
}


还有我从firestore获取的文件:

int selectedDate =
      0; // 0 for 'Today', 1 for 'Tomorrow', 2 for 'Historical Day'
  String selectedHistoricalDate =
      '2023-4-17'; // Replace this with the selected date.

  Stream<DocumentSnapshot> getPricesStream() {
    FirebaseFirestore firestore = FirebaseFirestore.instance;

    switch (selectedDate) {
      case 0:
        return firestore
            .collection('electricityPrices')
            .doc('todayData')
            .snapshots();
      case 1:
        return firestore
            .collection('electricityPrices')
            .doc('tomorrowData')
            .snapshots();
      case 2:
        return firestore
            .collection('electricityPrices')
            .doc('historicalData')
            .collection('dates')
            .doc(selectedHistoricalDate)
            .snapshots();
      default:
        throw Exception('Invalid selectedDate: $selectedDate');
    }
  }
...


文件的其余部分无关紧要。这是什么问题,我该如何解决?
我已经尝试了清理和重建,升级Flutter,降级和升级包,以及更多的事情,但仍然没有解决这个问题。

aij0ehis

aij0ehis1#

您似乎尚未在项目中初始化Firebase。你可以找到这样做的步骤here
完成链接文档中的所有步骤后,main函数应该如下所示:

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

import 'firebase_options.dart';
import 'ui/screens/main_screen.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
  runApp(const PrecioLuz());
}

字符串

相关问题