'Get all documents from a Firebase collection and set to static var Flutter
How to get all documents from firebase and set it to static var?
example
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
FirebaseAuth auth = FirebaseAuth.instance;
FirebaseFirestore firestore = FirebaseFirestore.instance;
final officeRef = firestore.collection("office");
class CompanyData {
***static Map<String, dynamic> /id/ = {
};***
}
from firebase like this FirebaseDB
How i can get result like this(***) so i can use it for another page..
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
class CompanyData {
FirebaseAuth auth = FirebaseAuth.instance;
FirebaseFirestore firestore = FirebaseFirestore.instance;
final officeRef = firestore.collection("office");
***
static Map<String, dynamic> office1 = {
'name': "OFFICE 1ST",
'code': "c_office1",
'latitude': -4.001,
'longitude': 100.001,
};
static Map<String, dynamic> office2 = {
'name': "OFFICE 2ND",
'code': "c_office2",
'latitude': -4.002,
'longitude': 100.002,
};
static Map<String, dynamic> office3 = {
'name': "OFFICE 3RD",
'code': "c_office3",
'latitude': -4.003,
'longitude': 100.003,
};
static Map<String, dynamic> etc.....
}
***
I'm sorry, I still very new to flutter and firebase.. so still many things i dont understand.
Solution 1:[1]
Create new file static_variable.dart
Create StaticVariable class like this:
class StaticVariable {
static bool? navigateRedeem;
static String? keywordJob;
static User? myUser;
}
Request api when open app, you can request api in SplashScreen or everywhere you want, it up to you,
After data response, set new data for StaticVariable:
//request api here: var res = await request .....
StaticVariable.myUser = res['user]
Then, you can call StaticVariable in everywhere in your project
Example:
static.dart
class StaticVariable {
static String? textHello;
}
main.dart
import 'package:demo/static.dart';
import 'package:flutter/material.dart';
import 'home.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
StaticVariable.textHello = 'Hihihi';
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: SafeArea(
child: Scaffold(
body: HomeScreen(),
),
),
);
}
}
home.dart
import 'package:demo/static.dart';
import 'package:flutter/material.dart';
class HomeScreen extends StatefulWidget {
const HomeScreen({Key? key}) : super(key: key);
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Text(StaticVariable.textHello ?? ''),
),
);
}
}
Similar with request api
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Saitoh Akira |
