gpt4 book ai didi

firebase - 如何在 Flutter Web 应用程序中将 Firebase.dart 与 Firebase Emulators FireStore db 结合使用?

转载 作者:行者123 更新时间:2023-12-03 03:00:14 24 4
gpt4 key购买 nike

我的应用程序是 Flutter Web 应用程序。它使用 package:firebase/firebase.dart .

我正在尝试使用 Firebase Emulators UI。

该应用程序托管在 Firebase 上并读取和写入 Firestore。但是当我使用 firebase emulators:start本地托管的应用程序位于 localhost:5000仍在访问生产 Firestore 数据库。

如何进行设置,以便本地运行的应用程序使用位于 localhost:8080 的 Firestore 模拟器?

我找到了类似 this 的帖子,但我没有使用 cloud_firestore.dart .我已经尝试过类似的步骤(请参阅下面的更新),但它们在我的上下文中不起作用。

这是我的 main.dart文件:

import 'package:algolia/algolia.dart';
import 'package:firebase/firebase.dart' as fb;
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:web_flutter/data/repository.dart';
import 'package:web_flutter/pages/auth/login.dart';
import 'package:web_flutter/pages/home/home_page.dart';
import 'package:web_flutter/services/authentication.dart';

Future<void> main() async {
try {
var app = fb.initializeApp(
apiKey: 'someAPIkey',
authDomain: 'some.domain.com',
databaseURL: 'https://somedomain.firebaseio.com',
projectId: 'myProj',
storageBucket: '',
messagingSenderId: 'someId',
appId: '1:9anAppId');

var searchClient = Algolia.init(
applicationId: 'anAppId',
apiKey: 'someAPIkey');

runApp(MyApp(
repository: Repository(app.firestore(), searchClient),
authentication: Authentication(app.auth())));
} on fb.FirebaseJsNotLoadedException catch (e) {
print(e);
}
}

class MyApp extends StatefulWidget {
final Repository repository;
final Authentication authentication;

const MyApp({Key key, this.authentication, this.repository})
: super(key: key);

@override
_MyAppState createState() => _MyAppState();
}

有什么想法吗?

谢谢!

更新:
我尝试了以下但得到相同的结果:
import 'package:algolia/algolia.dart';
import 'package:firebase/firebase.dart' as fb;
import 'package:firebase/src/firestore.dart' as fs;
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:web_flutter/data/repository.dart';
import 'package:web_flutter/pages/auth/login.dart';
import 'package:web_flutter/pages/home/home_page.dart';
import 'package:web_flutter/services/authentication.dart';

Future<void> main() async {
try {
var app = fb.initializeApp(
apiKey: 'someAPIkey',
authDomain: 'some.domain.com',
databaseURL: 'https://somedomain.firebaseio.com',
projectId: 'myProj',
storageBucket: '',
messagingSenderId: 'someId',
appId: '1:9anAppId');

var searchClient = Algolia.init(
applicationId: 'anAppId',
apiKey: 'someAPIkey');

// These two lines are new:
var db = app.firestore();
db.settings(fs.Settings(host: 'localhost:8080', ssl: false));

runApp(MyApp(
repository: Repository(db, searchClient),
authentication: Authentication(app.auth())));
} on fb.FirebaseJsNotLoadedException catch (e) {
print(e);
}
}

最佳答案

关键是导入 dart:html让我可以访问 window .从那里我能够做一些类似于 Firebase 文档中使用的 JavaScript 的事情。

import 'dart:html';
import 'package:algolia/algolia.dart';
import 'package:firebase/firebase.dart' as fb;
import 'package:firebase/firestore.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:web_flutter/data/repository.dart';
import 'package:web_flutter/pages/auth/login.dart';
import 'package:web_flutter/pages/home/home_page.dart';
import 'package:web_flutter/services/authentication.dart';

Future<void> main() async {
try {
var app = fb.initializeApp(firebaseConfig);

if (window.location.hostname == 'localhost') {
app.firestore().settings(Settings(
host: '0.0.0.0:8080',
ssl: false,
));
}
不知道楼主会不会 0.0.0.0适合所有情况下的每个人。我从模拟器启动时列出的主机那里得到了这个。

关于firebase - 如何在 Flutter Web 应用程序中将 Firebase.dart 与 Firebase Emulators FireStore db 结合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62162480/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com