firebase - firebase 与颤振应用程序没有连接
问题描述
问题是什么 ?之后加载小部件正在工作,而不是连接小部件正在运行我该如何解决这个问题
我的代码:
import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:marwa_app/component/myResponsiveLibrary.dart'; import 'package:marwa_app/component/Logo.dart'; import 'package:marwa_app/component/MyDrawer.dart'; import 'package:geolocator/geolocator.dart'; import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_core/firebase_core.dart';
class Reporting extends StatefulWidget { @override
_ReportingState createState() => _ReportingState(); }
class _ReportingState extends State<Reporting> { final formKey = GlobalKey<FormState>(); final scaffoldKey = GlobalKey<ScaffoldState>();
BoxDecoration boxDecoration() {
return BoxDecoration(
color: MainModel().mainColor,
borderRadius: BorderRadius.all(Radius.circular(40.0)),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.15),
spreadRadius: 5,
blurRadius: 5,
offset: Offset(2, 2), // changes position of shadow
),
],
); }
TextStyle titleStyle() {
return TextStyle(
color: MainModel().whiteColor,
fontSize: MainModel().setFont(MainModel().largeFont, getWidth())); }
TextStyle textStyle() {
return TextStyle(
color: MainModel().whiteColor,
fontSize: MainModel().setFont(MainModel().middleFont, getWidth()),
); }
double getWidth() {
return MediaQuery.of(context).size.width; }
final _name = TextEditingController(); final _phone = TextEditingController(); final _emType = TextEditingController(); double lat; double lon; bool _initialized = false; bool _error
= false;
void initializeFlutterFire() async {
try {
await Firebase.initializeApp();
setState(() {
_initialized = true;
});
} catch(e) {
// Set `_error` state to true if Firebase initialization fails
setState(() {
_error = true;
});
} }
@override void initState() {
initializeFlutterFire();
super.initState(); } @override Widget build(BuildContext context) {
if(_error) {
return notConnection();
}
if (!_initialized) {
return Loading();
}
return isConnection(); }
Widget isConnection() {
final widthScreen = MediaQuery.of(context).size.width;
return Scaffold(
key: scaffoldKey,
backgroundColor: MainModel().mainColor,
appBar: AppBar(
backgroundColor: Color(0xff323266),
leading: IconButton(
icon: Icon(
Icons.notifications_active,
color: MainModel().thirdColor,
),
onPressed: () => {}),
title: Logo(),
centerTitle: true,
actions: [
Builder(
builder: (context) => IconButton(
icon: Icon(
Icons.menu,
color: MainModel().thirdColor,
),
onPressed: () => Scaffold.of(context).openEndDrawer(),
tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
),
),
],
),
endDrawer: Drawer(
child: MyDrawer(),
),
body: ListView(
padding: EdgeInsets.only(
top: MainModel()
.setPadding(MainModel().largePadding, widthScreen)),
children: [
Form(
key: formKey,
child: Column(
children: [
Directionality(
textDirection: TextDirection.rtl,
child: Container(
padding: EdgeInsets.symmetric(horizontal: 20),
margin: EdgeInsets.symmetric(horizontal: 20),
alignment: Alignment.centerRight,
decoration: boxDecoration(),
child: TextFormField(
controller: _name,
textDirection: TextDirection.rtl,
autofocus: true,
decoration: InputDecoration(
border: InputBorder.none,
hintStyle: textStyle(),
hintText: 'الاسم الرباعي'),
validator: (value) {
if (value.isEmpty) {
return "يرجى ادخل الاسم";
} else {
return null;
}
},
),
),
),
Padding(
padding: EdgeInsets.only(
top: MainModel().setPadding(
MainModel().middlePadding, widthScreen)),
),
Directionality(
textDirection: TextDirection.rtl,
child: Container(
padding: EdgeInsets.symmetric(horizontal: 20),
margin: EdgeInsets.symmetric(horizontal: 20),
alignment: Alignment.centerRight,
decoration: boxDecoration(),
child: TextFormField(
controller: _phone,
keyboardType: TextInputType.number,
textDirection: TextDirection.rtl,
autofocus: true,
decoration: InputDecoration(
border: InputBorder.none,
hintStyle: textStyle(),
hintText: 'رقم الهاتف'),
validator: (value) {
if (value.isEmpty) {
return "يرجى ادخال رقم الهاتف";
} else {
return null;
}
},
),
),
),
Padding(
padding: EdgeInsets.only(
top: MainModel().setPadding(
MainModel().middlePadding, widthScreen)),
),
Directionality(
textDirection: TextDirection.rtl,
child: Container(
padding: EdgeInsets.symmetric(horizontal: 20),
margin: EdgeInsets.symmetric(horizontal: 20),
alignment: Alignment.centerRight,
decoration: boxDecoration(),
child: TextFormField(
controller: _emType,
textDirection: TextDirection.rtl,
autofocus: true,
decoration: InputDecoration(
border: InputBorder.none,
hintStyle: textStyle(),
hintText: 'نوع الابلاغ'),
validator: (value) {
if (value.isEmpty) {
return "يرجى ادخال نوع الابلاغ (الحالة)";
} else {
return null;
}
},
),
),
),
Padding(
padding: EdgeInsets.only(
top: MainModel().setPadding(
MainModel().middlePadding, widthScreen)),
),
Container(
child: GestureDetector(
onTap: () async {
bool isLocationServiceEnabled =
await Geolocator.isLocationServiceEnabled();
if (isLocationServiceEnabled == true) {
Position position =
await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
lon = position.longitude;
lat = position.latitude;
} else {
print(
'امنح التطبيق من الوصول الى موقع... ثم اعد الارسال');
}
},
child: Image.asset('images/location.png')),
),
Padding(
padding: EdgeInsets.only(
bottom: MainModel().setPadding(
MainModel().largePadding * 2, widthScreen))),
RaisedButton(
onPressed: () {
Firebase.initializeApp();
if (formKey.currentState.validate()) {
// save data in firebase
FirebaseFirestore.instance
.collection('Data')
.doc()
.set({
'name': _name,
'phone': _phone,
'em-type': _emType,
});
scaffoldKey.currentState.showSnackBar(SnackBar(
content: Text('تم ارسال بياناتك .... '
'سيتم التواصل معك من الجهات المختصة')));
}
},
child: Text('ارسال'),
)
],
))
],
)); }
Widget notConnection() {
final widthScreen = MediaQuery.of(context).size.width;
return Scaffold(
key: scaffoldKey,
backgroundColor: MainModel().mainColor,
appBar: AppBar(
backgroundColor: Color(0xff323266),
leading: IconButton(
icon: Icon(
Icons.notifications_active,
color: MainModel().thirdColor,
),
onPressed: () => {}),
title: Logo(),
centerTitle: true,
actions: [
Builder(
builder: (context) => IconButton(
icon: Icon(
Icons.menu,
color: MainModel().thirdColor,
),
onPressed: () => Scaffold.of(context).openEndDrawer(),
tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
),
),
],
),
endDrawer: Drawer(
child: MyDrawer(),
),
body: ListView(
padding: EdgeInsets.only(
top: MainModel()
.setPadding(MainModel().largePadding, widthScreen)),
children: [
Container(
child: Center(
child: Text('not Connection of Database'),
),
)
])); }
Widget Loading() {
final widthScreen = MediaQuery.of(context).size.width;
return Scaffold(
key: scaffoldKey,
backgroundColor: MainModel().mainColor,
appBar: AppBar(
backgroundColor: Color(0xff323266),
leading: IconButton(
icon: Icon(
Icons.notifications_active,
color: MainModel().thirdColor,
),
onPressed: () => {}),
title: Logo(),
centerTitle: true,
actions: [
Builder(
builder: (context) => IconButton(
icon: Icon(
Icons.menu,
color: MainModel().thirdColor,
),
onPressed: () => Scaffold.of(context).openEndDrawer(),
tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
),
),
],
),
endDrawer: Drawer(
child: MyDrawer(),
),
body: Container(
child: Center(
child: Text('Loading'),
),
)
); } }
我在编译器上没有任何错误,但没有连接 firebase。只是我想从 textfeild 获取数据并保存在 firebase 中。我的数据是(姓名、电话、位置、紧急情况类型)。
解决方案
推荐阅读
- json - 如何使用可用的属性从 json 中检索特定数据
- java - 如何根据 Jenkins 的条件运行 Maven 测试类
- php - 上传到服务器时更改文件的文件名
- json - Flutter DropDownButton 使用 FutureBuilder 进行 JSON 响应
- python - 在cupy中反转稀疏矩阵(python中的CUDA GPU编程)
- apache - 用于重定向所有内部链接的 htaccess 代码
- python-3.x - 在 Trimesh geoview 中,颜色图和颜色条显示不正确
- xml - VTD-XML 不返回正确的值,如何修改查询以获得正确的结果?
- pdf - 如何通过单击 mpdf、php 中的按钮在新选项卡中打印多个 pdf
- javascript - at max right scroll >— 隐藏右侧部分而不是左侧部分