- 初始化核心:https://firebase.flutter.dev/docs/overview#initializing-flutterfire
- 使用firestore插件:https://firebase.flutter.dev/docs/firestore/usage
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(
Main(firebaseApp: Firebase.initializeApp()),
);
}
class Main extends StatelessWidget {
final Future<FirebaseApp> firebaseApp;
const Main({this.firebaseApp});
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: firebaseApp,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
final firestoreInstance = FirebaseFirestore.instanceFor(
app: snapshot.data,
);
return MyWidget();
}
return CircularProgressIndicator();
},
);
}
}
但是当我运行下面的测试时,我收到了以下消息:
"构建Builder(dirty)时抛出了以下FirebaseException: [core/no-app] 没有创建Firebase App '[DEFAULT]' - 请调用Firebase.initializeApp()"
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
class MockFirebaseApp extends Mock implements FirebaseApp {}
void main() {
FirebaseApp firebaseApp;
setUp(() async {
TestWidgetsFlutterBinding.ensureInitialized();
firebaseApp = MockFirebaseApp();
});
group('Main', () {
testWidgets('Loads my widget', (WidgetTester tester) async {
await tester.runAsync(() async {
await tester.pumpWidget(
Main(firebaseApp: Future.value(firebaseApp)),
);
expect(find.byType(CircularProgressIndicator), findsOneWidget);
});
});
});
}