我最新的flutter fire设置
void main() {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
const bool USE_EMULATOR = true;
if (USE_EMULATOR) {
// [Firestore | localhost:8080]
FirebaseFirestore.instance.settings = const Settings(
host: 'localhost:8080',
sslEnabled: false,
persistenceEnabled: false,
);
// [Authentication | localhost:9099]
await FirebaseAuth.instance.useEmulator('http://localhost:9099');
// [Storage | localhost:9199]
await FirebaseStorage.instance.useEmulator(
host: 'localhost',
port: 9199,
);
}
}
确保您的主机和端口与firebase emulators:start 匹配
注意:在main.dart 现在您可以随时提供'localhost'
await FirebaseAuth.instance.useEmulator('http://localhost:9099');
因为如果它在 android 上运行,它会自动更改为“10.0.2.2”
长话短说!
如需最新指南,请关注https://firebase.flutter.dev/docs/firestore/usage#emulator-usage
老但是,黄金。详细配置。(过时)
步骤 1 [在 main.dart 中设置 firestore]
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 步 [初始化 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 应用
完成!
BONUS [将导出数据导入到 Firestore 模拟器]
当您停止 firestore 模拟器时,firestore 中的所有数据都将消失。
所以如果你想从哪里继续,也许在停止模拟器之前
你剩下的可以像这样导出firestore模拟器数据
firebase emulators:export ../data(../data 可以是你想要的任何路径?)
加载导出的数据
firebase emulators:start --import ../data
您可以为不同的情况保存 Firestore 模拟器的不同状态,例如
firebase emulators:start --import ../initialData
firebase emulators:start --import ../otherStateData
❤️ 自己的注意事项将 dart 用于 firebase 功能 ❤️
如果您想将 dart 用于 firebase 功能,可以关注此https://github.com/pulyaevskiy/firebase-functions-interop
我为自己发现了一件好事,可以检测您的功能是在 emulator 还是 production 中运行,您可以阅读更多 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"
);
}
}
);
函数/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();
}
}