我已通过点击this链接将firebase配置为在本地运行以使用模拟器进行调试。
现在,我也希望能够运行连接到本地主机的应用程序以调试触发器。是否可以通过配置flutter应用程序以使用localhost来实现此目的?
我的模拟器运行如下:
答案 0 :(得分:10)
STEP 1 [在main.dart中的颤振中设置消防站]
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized(); <--- Important!
await Firestore.instance.settings(
host: '192.168.1.38:5002', <--- Make sure to put your local ip
sslEnabled: false); it will not work if you use 'localhost:5002'
Google it "how to find my local ip"
}
第1步[在main.dart中的flutter中设置Firestore],用于更新版本的Firebase
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized(); <--- Important!
String host = Platform.isAndroid ? '10.0.2.2:5002' : 'localhost:5002';
await FirebaseFirestore.instance.settings = Settings(
host: host,
sslEnabled: false,
);
}
第2步[init firebase项目]
firebase init
第3步[配置firestore仿真器,例如firebase.json]
"emulators": {
"ui": {
"enabled": true,
"host": "localhost",
"port": 4000
},
"functions": {
"port": 5001
},
"firestore": {
"host": "0.0.0.0", <------ Make sure to set it "0.0.0.0"
"port": 5002
},
}
第4步[运行模拟器和Flutter应用]
firebase emulators:start
flutter run
在iOS模拟器和Android模拟器上均可使用
P.S:尝试重新启动Firestore模拟器或/和Flutter应用
当您停止Firestore模拟器时,Firestore中的所有数据都将消失。 因此,如果要从哪里继续,也许在停止模拟器之前 您剩下的可以像这样导出Firestore模拟器数据
firebase emulators:export ../data
(../数据可以是您想要的任何路径?)
加载导出的数据
firebase emulators:start --import ../data
您可以在不同情况下保存Firestore模拟器的不同状态,例如
firebase emulators:start --import ../initialData
firebase emulators:start --import ../otherStateData
如果您想将dart用于firebase功能,则可以遵循此https://github.com/pulyaevskiy/firebase-functions-interop
我发现一个好东西,可以检测您的功能是在模拟器还是生产版本中运行,您可以阅读更多here
functions/index.js
export const prepopulateFirestoreEmulator = functions.https.onRequest(
(request, response) => {
if (process.env.FUNCTIONS_EMULATOR && process.env.FIRESTORE_EMULATOR_HOST) {
// TODO: prepopulate firestore emulator from 'yourproject/src/sample_data.json
response.send('Prepopulated firestore with sample_data.json!');
} else {
response.send(
"Do not populate production firestore with sample_data.json"
);
}
}
);
functions / index.dart
import 'package:firebase_functions_interop/firebase_functions_interop.dart';
import 'package:node_interop/node.dart';
import 'package:node_interop/util.dart';
void devPrepopulateCollections(ExpressHttpRequest request) {
var env =
new Map<String, String>.from(dartify(process.env)); // <-- important part
if (env.containsKey('FUNCTIONS_EMULATOR') &&
env.containsKey('FIRESTORE_EMULATOR_HOST')) {
// TODO: prepopulate firestore emulator from 'yourproject/src/sample_data.json
request.response
..write("Prepopulated firestore with sample_data.json!")
..close();
} else {
request.response
..write("Do not populate production firestore with sample_data.json")
..close();
}
}
答案 1 :(得分:1)
在仔细研究了文档here之后,我通过在Firestore实例上配置主机设置来使其正常工作:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:tracker/screens/screens.dart';
void main() async {
// This will set the app to communicate with localhost
await Firestore.instance.settings(host: "10.0.2.2:8080", sslEnabled: false);
runApp(AppSetupScreen());
}
注意:这仅适用于仿真器,不适用于物理设备。
答案 2 :(得分:1)
对于 cloud_firestore: ^0.14.1+2
,不要使用 FirebaseFirestore.instance.settings
使用这个 -
FirebaseFunctions.instance.useFunctionsEmulator(
origin: "http://localhost:5001",
);
如果设备是 android,它会在内部处理设置 10.0.2.2
。
您的主块应如下所示
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseFunctions.instance.useFunctionsEmulator(
origin: "http://localhost:5001",
);
runApp(MyApp());
}
答案 3 :(得分:1)
适用于 iOS 和 Android 设备
在firebase.json
{
// ...other configs
"emulators": {
"functions": {
"port": 5001,
"host": "0.0.0.0" // must have
},
"firestore": {
"port": 8080,
"host": "0.0.0.0" // must have
},
}
}
您用于云功能和firestore的IP地址应该相同
// The FirebaseFunctions config
// ! You need to replace the placeholder with your IP address below:
FirebaseFunctions.instance.useFunctionsEmulator(origin: 'http://<YOUR_IP_ADDRESS>:5001');
// The Firestore config
// ! You need to replace the placeholder with your IP address below:
FirebaseFirestore.instance.settings = Settings(
host: '<YOUR_IP_ADDRESS>:8080',
sslEnabled: false,
persistenceEnabled: false,
);
答案 4 :(得分:0)
看起来我已经将ios连接到localhost:8080,但是db的运行速度非常慢,而且我也没有注意到文件中的任何日志。 @UsmanZaheer,您能告诉我它什么时候写日志并且工作速度很快吗?
步骤:
firebase初始化
将ini创建的链接添加到函数中的package.json;
“ firestore”:{ “ rules”:“ firestore.rules”, “ indexes”:“ firestore.indexes.json” },
firebase模拟器:启动
在main()中写
await Firestore.instance.settings(
host: 'http://localhost:8080',
sslEnabled: false,
persistenceEnabled: false,
timestampsInSnapshotsEnabled: true
).catchError((e) => print(e));
答案 5 :(得分:0)
您的main.dart
应该如下所示:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firestore.instance
.settings(
host: 'http://localhost:8080',
sslEnabled: false,
persistenceEnabled: false,
)
.catchError((e) => print(e));
//
// ...
//
runApp(App(...));
}
在您的firebase.json
文件中
"emulators": {
"firestore": {
"host": "localhost",
"port": 8080
},
...
}
您还应该在终端中设置以下内容:
export FIRESTORE_EMULATOR_HOST=localhost:8080
然后运行
firebase emulators:start
答案 6 :(得分:0)
添加到correct answer by @Sultanmyrza
Platform
要求dart:io/dart:html
互斥,因此要检查我使用的平台kIsWeb
FirebaseFirestore __firestore;
FirebaseFirestore get _firestore {
if (__firestore != null) {
return __firestore;
}
debugPrint('isFirebaseEmulator: $isFirebaseEmulator');
__firestore = FirebaseFirestore.instance;
if (isFirebaseEmulator) {
__firestore.settings = const Settings(
host: kIsWeb ? 'localhost:8080' : '10.0.2.2:8080',
sslEnabled: false,
);
}
return __firestore;
}
答案 7 :(得分:0)
最新更新:要将flutter应用程序连接到本地Firebase仿真器套件,请按照this官方说明进行配置。