无法将颤振网页应用程序链接到Firebase项目/无限加载



当我尝试将我的Flutter web项目链接到我的Firebase时,我有一个问题。我添加元素、导入和SDK,当我触摸main时。即使代码没有显示任何错误,当我用google从IDE启动应用程序时,我也会面临一个无限的加载屏幕。有时甚至没有蓝色的加载条。所有的SDK, IDE和框架都是最新的稳定版本。

我只使用这个应用程序配置了一个firebase项目,并初始化了一个firestore数据库,就像我在CRUD中看到的那样。

CRUD链接:https://www.youtube.com/watch?v=Ue_dIKOMcb4&t=1009s

但我不认为它来自项目,因为我甚至不能在默认的扑动计数器新应用程序中初始化Firebase。

在我的index。html中,在firebase配置段落中,我的API密钥用红色下划线下划线

也许我在项目中使用了错误的方法来初始化Firebase函数,所以请有人告诉我如何做。

这是我的pubspec.yaml:

name: flutter_web_diary
description: A new Flutter project.
version: 1.0.0+1
environment:
sdk: ">=2.6.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
provider: ^4.0.4
firebase_core: "^1.7.0"
cloud_firestore: "^2.5.3"
cupertino_icons: ^0.1.2
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true

Myidex.HTML:

<!DOCTYPE html>
<html lang="en">
<head>
<!--
If you are serving your web app in a path other than the root, change the
href value below to reflect the base path you are serving from.
The path provided below has to start and end with a slash "/" in order for
it to work correctly.
For more details:
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
This is a placeholder for base href that will be replaced by the value of
the `--base-href` argument provided to `flutter build`.
-->
<base href="$FLUTTER_BASE_HREF">
<meta charset="UTF-8">
<meta content="IE=Edge" http-equiv="X-UA-Compatible">
<meta name="description" content="A new Flutter project.">
<!-- iOS meta tags & icons -->
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-title" content="flutter_application_1">
<link rel="apple-touch-icon" href="icons/Icon-192.png">
<title>flutter_application_1</title>
<link rel="manifest" href="manifest.json">
</head>
<body>

<script>
import { initializeApp } from "firebase/app";
import { } from "https://www.gstatic.com/firebasejs/9.1.3/firebase-app.js"
import { } from "https://www.gstatic.com/firebasejs/9.1.3/firebase-firestore.js"
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "...",
authDomain: "...",
databaseURL: "...",
projectId: "...",
storageBucket: "...",
messagingSenderId: "...",
appId: "..."
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
var serviceWorkerVersion = null;
var scriptLoaded = false;
function loadMainDartJs() {
if (scriptLoaded) {
return;
}
scriptLoaded = true;
var scriptTag = document.createElement('script');
scriptTag.src = 'main.dart.js';
scriptTag.type = 'application/javascript';
document.body.append(scriptTag);
}
if ('serviceWorker' in navigator) {
// Service workers are supported. Use them.
window.addEventListener('load', function () {
// Wait for registration to finish before dropping the <script> tag.
// Otherwise, the browser will load the script multiple times,
// potentially different versions.
var serviceWorkerUrl = 'flutter_service_worker.js?v=' + serviceWorkerVersion;
navigator.serviceWorker.register(serviceWorkerUrl)
.then((reg) => {
function waitForActivation(serviceWorker) {
serviceWorker.addEventListener('statechange', () => {
if (serviceWorker.state == 'activated') {
console.log('Installed new service worker.');
loadMainDartJs();
}
});
}
if (!reg.active && (reg.installing || reg.waiting)) {
// No active web worker and we have installed or are installing
// one for the first time. Simply wait for it to activate.
waitForActivation(reg.installing || reg.waiting);
} else if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) {
// When the app updates the serviceWorkerVersion changes, so we
// need to ask the service worker to update.
console.log('New service worker available.');
reg.update();
waitForActivation(reg.installing);
} else {
// Existing service worker is still good.
console.log('Loading app from service worker.');
loadMainDartJs();
}
});
// If service worker doesn't succeed in a reasonable amount of time,
// fallback to plaint <script> tag.
setTimeout(() => {
if (!scriptLoaded) {
console.warn(
'Failed to load app from service worker. Falling back to plain <script> tag.',
);
loadMainDartJs();
}
}, 4000);
});
} else {
// Service workers not supported. Just drop the <script> tag.
loadMainDartJs();
}
</script>
</body>
</html>

和我的main.dart:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:flutter_web_diary/diary_card.dart';
import 'package:flutter_web_diary/diary_entry_model.dart';
import 'package:flutter_web_diary/top_bar_title.dart';
import 'package:provider/provider.dart';
import 'diary_entry_page.dart';
// Import the firebase_core plugin
import 'package:firebase_core/firebase_core.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
/// We are using a StatefulWidget such that we only create the [Future] once,
/// no matter how many times our widget rebuild.
/// If we used a [StatelessWidget], in the event where [App] is rebuilt, that
/// would re-initialize FlutterFire and make our application re-enter loading state,
/// which is undesired.
class App extends StatefulWidget {
// Create the initialization Future outside of `build`:
@override
_AppState createState() => _AppState();
}
class _AppState extends State<App> {
/// The future is part of the state of our widget. We should not call `initializeApp`
/// directly inside [build].
final Future<FirebaseApp> _initialization = Firebase.initializeApp();
@override
Widget build(BuildContext context) {
return FutureBuilder(
// Initialize FlutterFire:
future: _initialization,
builder: (context, snapshot) {
// Check for errors
if (snapshot.hasError) {
return Text(snapshot.error.toString());
}
// Once complete, show your application
if (snapshot.connectionState == ConnectionState.done) {
return MyApp();
}
// Otherwise, show something whilst waiting for initialization to complete
return CircularProgressIndicator();
},
);
}
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
// Refer to https://firebase.flutter.dev
final diaryCollection = FirebaseFirestore.instance.collection('diaries');
final diaryStream = diaryCollection.snapshots().map((snapshot) {
return snapshot.docs.map((doc) => DiaryEntry.fromDoc(doc)).toList();
});
return StreamProvider<List<DiaryEntry>>(
create: (_) => diaryStream,
child: MaterialApp(
title: 'My Diary',
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.indigo).copyWith(secondary: Colors.pink),
),
initialRoute: '/',
routes: {
'/': (context) => MyHomePage(),
'/new-entry': (context) => DiaryEntryPage.add(),
},
),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
final diaryEntries = Provider.of<List<DiaryEntry>>(context);
return Scaffold(
appBar: AppBar(
bottom: PreferredSize(
preferredSize: Size.fromHeight(94.0),
child: TopBarTitle('Diary Entries'),
),
elevation: 0,
),
body: Center(
child: SizedBox(
width: MediaQuery.of(context).size.width * 3 / 5,
child: ListView(
children: <Widget>[
SizedBox(height: 40),
if (diaryEntries != null)
for (var diaryData in diaryEntries)
DiaryCard(diaryEntry: diaryData),
if (diaryEntries == null)
Center(child: CircularProgressIndicator()),
],
),
),
),
floatingActionButtonLocation: FloatingActionButtonLocation.endTop,
floatingActionButton: FloatingActionButton(
elevation: 1.5,
onPressed: () => Navigator.of(context).pushNamed('/new-entry'),
tooltip: 'Add To Do',
child: Icon(Icons.add),
backgroundColor: Theme.of(context).colorScheme.secondary,
),
);
}
}

对于web中的firebase有一个不同的包。请检查一下这个Firebase_core_web

好的,我刚刚改变了在main。dart中初始化项目的方式

:

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

而不是:

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

我把我的firestore数据库设置为Test模式。

相关内容

  • 没有找到相关文章

最新更新